[英]How to plot average in y axis for each x value in pyplot
I am using matplotlib.pyplot to visualize my data. 我正在使用matplotlib.pyplot可视化我的数据。 In pandas I have columns 'hour' and
'favourite_count'
. 在熊猫中,我有'hour'和
'favourite_count'
。 hour has values form 0 to 24. favourite_count
is a continuous variable. hour的值形式为0到
favourite_count
是一个连续变量。 What I want is to plot a bar chart which visualizes the average favourite_count
for each hour. 我想要的是绘制一个条形图,该条形图将每小时的平均
favourite_count
数量可视化。 Currently I am plotting a basic graph as below. 目前,我正在绘制一个基本图,如下所示。 In the y axis this plots the sum / maximum of
favourite_count
for each hour (I am not sure which). 在y轴上,它绘制了每个小时的
favourite_count
的总和/最大值(我不确定是哪个)。 How can I plot a graph that visualizes hour vs average_favorite_count_for_hour
如何绘制可视化小时与
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()
Perform an averaging step by adding this line just before plotting: 通过在绘制之前添加以下行来执行平均步骤:
result = result.groupby('hour').mean()
then plot as below: 然后绘制如下:
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()
Note the x axis is now the index. 请注意,x轴现在是索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.