gpt4 book ai didi

python - 如何使用分配不同颜色的散点图绘制多个分类数据?

转载 作者:行者123 更新时间:2023-12-01 07:49:31 25 4
gpt4 key购买 nike

我有两列分类变量,我想根据相同的 x 轴绘制每一列。

例如,对于以下 csv 文件,我想根据状态绘制类型并分配颜色。例如,类型 a 表示为“x”,类型“b”表示为“o”。如果类型“a”的状态为“yes”,则其状态为绿色,否则为红色,类型“b”的状态相同。

start_time,type,status
2017-08-21 13:41:39.899,a,yes
2017-08-21 14:11:21.283,b,yes
2017-08-21 14:22:09.590,b,no
2017-08-21 14:27:56.175,b,yes
2017-08-21 15:51:07.950,a,yes
2017-08-21 17:37:37.959,a,no
2017-08-21 18:53:37.040,b,yes
2017-08-21 18:54:34.282,a,no
2017-08-21 20:36:36.180,b,yes
2017-08-21 20:42:23.579,a,no

我尝试使用 matplotlib,plt.scatter() 但无法得到我想要的。请帮忙。另外,我在 pandas 数据框中使用了开始时间作为索引。编辑 :X轴是start_time

最佳答案

假设您想在 x=start_timey='y' 上绘制散点图,可以使用 sns.scatterplot:

fig, ax = plt.subplots(figsize=(10,6))
sns.scatterplot(x='start_time', y='y',
style='type',
hue='status',
data=df,
ax=ax)
ax.set_xlim(df.start_time.min(), df.start_time.max())
plt.show()

这给出了

enter image description here

关于python - 如何使用分配不同颜色的散点图绘制多个分类数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56296304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com