gpt4 book ai didi

python - 在 Python 中绘制特定列值

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:55 25 4
gpt4 key购买 nike

我得到了每年每个月 (1-12) HOUR (0-23) 重复的值,例如:

Year    Month     HOUR  NAME            RATE
2010 1 0 Big 2
2010 1 0 Channelview 4
2010 1 0 Cottonwood 12
2010 1 1 Big 4
2010 1 1 Channelview 9
2010 1 1 Cottonwood 11
.
.
.
2010 2 0 Big 6
2010 2 0 Channelview 10
2010 2 0 Cottonwood 17
.
.
2013 1 0 Big 4
2013 1 0 Channelview 9
2013 1 0 Cottonwood 11

我想按 HOUR(x 轴)为一年中每个月的名称绘制此 RATE 数据(y 轴)(持续 7 年)。有没有一种方法可以通过 .groupby.loc 来执行此操作,而无需根据年份和月份创建额外的数据帧?

最佳答案

您可以使用 groupby 执行此操作:

for name, data in df.groupby('NAME'):
plt.plot(data['HOUR'], data['RATE'], label=name)

plt.xlabel('Hour')
plt.ylabel('Rate')
plt.legend()
plt.show()

enter image description here

[编辑] 根据您的评论,要为每个名称创建并保存单独的绘图,您可以执行以下操作:

for name, data in df.groupby('NAME'):
plt.plot(data['HOUR'], data['RATE'], label=name)
plt.xlabel('Hour')
plt.ylabel('Rate')
plt.legend()
plt.savefig('plot_{}.png'.format(name))
plt.close()

关于python - 在 Python 中绘制特定列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53164305/

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