如何在matplotlib.pyplot中散布一个x数据图与几个不相等的y数据图 [英] How to scatter plot one x data versus several unequal y data plots in matplotlib.pyplot

查看:60
本文介绍了如何在matplotlib.pyplot中散布一个x数据图与几个不相等的y数据图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有不同长度的 x 和 y 元组.如何在 matplotlib 中绘制以下内容?

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)

谢谢

推荐答案

IIUC 你需要将你的 x 列表扩展到 y 维度,然后将得到的列表扁平化并放入plt.scatter:

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)

注意:您可以使用 itertools.chain.from_iterable()那个更快的问题

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

这篇关于如何在matplotlib.pyplot中散布一个x数据图与几个不相等的y数据图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆