2

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,4.1,4.4411])
plt.scatter(x,y)

Thank you

gogo
  • 219
  • 4
  • 13
  • 1
    How do you want the data to be interpreted? – moooeeeep Jan 11 '16 at 21:30
  • I am not sure what you mean by that. On a side not, I do not have a preference for any kind of solution to the question. – gogo Jan 11 '16 at 21:33
  • @gogo - As you've currently written the question, there are a near-infinite number of ways the data could be plotted. Basically, each point is an x,y pair. How do you want the coordinates in `x` and `y` combined? If they're not the same length, there's more than one way to do it. – Joe Kington Jan 11 '16 at 21:35
  • @Joe, is there some other method than Anton's you would like to share? Thanks – gogo Jan 11 '16 at 21:46

1 Answers1

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

Community
  • 1
  • 1
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93