如何在一个散点图上绘制多个数据集在各自的列上

2024-09-30 22:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有5个数组,每个数组包含30个值。我想在一个散点图中,将每个数组绘制在它自己的列上。 所以我想最后得到一个散点图,有5列,每列有30个数据点。我不希望数组重叠,这是我现在在代码中遇到的问题

plt.scatter(y,Coh1mean40,label='1', c='r')
plt.scatter(y, Coh75mean40,label='75', c='b')
plt.scatter(y,Coh05mean40,label='50', c='y')
plt.scatter(y,Coh25mean40,label='25', c='g')
plt.scatter(y,Coh00mean40,label='0')
plt.legend()
plt.show()

这段代码给了我一个散点图,上面有所有的数据点,但它们都重叠,没有明显的列

y只是一个包含30个数字的列表,因为plt.scatter函数需要两个参数。 Coh1mean40、Coh75mean40等都是数组,每个数组中包含[0.435、0.56、0.645…]30个值


Tags: 数据代码show绘制plt数组labellegend
1条回答
网友
1楼 · 发布于 2024-09-30 22:19:29

您需要为每个数组的每个调用指定唯一的y,因为每个数组将位于不同的“列”中。更好的方法是调用它x,因为您使用第一个参数定义每个点的x值

x = np.ones(30)

plt.scatter(0 * x,Coh1mean40,label='1', c='r')
plt.scatter(1 * x, Coh75mean40,label='75', c='b')
plt.scatter(2 * x,Coh05mean40,label='50', c='y')
plt.scatter(3 * x,Coh25mean40,label='25', c='g')
plt.scatter(4 * x,Coh00mean40,label='0')
plt.legend()
plt.show()

但是,您可能希望转而研究Seaborn,特别是^{}

相关问题 更多 >