How to scatter plot one x data versus several unequal y data plots in matplotlib.pyplot

▼魔方 西西 提交于 2019-12-23 22:26:54

问题


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


回答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)

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



来源:https://stackoverflow.com/questions/34731398/how-to-scatter-plot-one-x-data-versus-several-unequal-y-data-plots-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!