我正在使用matplotlib.pyplot可视化我的数据。在熊猫中,我有“小时”和“ 'favourite_count'
”列。 hour的值形式为0到24。favourite_count
是一个连续变量。我想要的是绘制一个条形图,以可视化每个小时的平均favourite_count
。目前,我正在绘制一个基本图,如下所示。在y轴上,它绘制了每小时favourite_count
的总和/最大值(我不确定是哪一个)。如何绘制可视化小时与average_favorite_count_for_hour
plt.bar(result['hour'], result['favourite_count'])
plt.xlabel('hour')
plt.ylabel('favourite_count')
plt.title('hour vs popularity', y=1.1)
plt.grid()
plt.show()
答案 0 :(得分:2)
通过在绘制之前添加以下行来执行平均步骤:
result = result.groupby('hour').mean()
然后绘制如下图:
plt.bar(result.index, result['favourite_count'])
plt.xlabel('hour')
plt.ylabel('favourite_count')
plt.title('hour vs popularity', y=1.1)
plt.grid()
plt.show()
请注意,x轴现在是索引。