Skip to content Skip to sidebar Skip to footer

How To Scatter Plot One X Data Versus Several Unequal Y Data Plots In Matplotlib.pyplot

Basically I have x versus y tuple of different length. How can I plot the following in matplotlib? x=[1,2,3,4] y=([1,1.1,1.4,0.9,0.8],[2.1,2.2,2.3],[3.1,3.3],[4.4,4.5,4.3,4.22,4.2,

Solution 1:

IIUC you need to expand your x list to y dimension and then flat obtained list and put in plt.scatter:

x=[1,2,3,4]
y=([1,1.1,1.4,0.9,0.8],[2.1,2.2,2.3],[3.1,3.3],[4.4,4.5,4.3,4.22,4.2,4.1,4.4411])

w = [[x[i]] * len(y[i]) for i in range(len(y))]

In [555]: w
Out[555]: [[1, 1, 1, 1, 1], [2, 2, 2], [3, 3], [4, 4, 4, 4, 4, 4, 4]]

x_to_plot = [item for sublist in w for item in sublist]
y_to_plot = [item for sublist in y for item in sublist]
plt.scatter(x_to_plot, y_to_plot)

enter image description here

Note: You could use itertools.chain.from_iterable() to make flatten lists from that question which is a faster

Post a Comment for "How To Scatter Plot One X Data Versus Several Unequal Y Data Plots In Matplotlib.pyplot"