简体   繁体   English

如何在pyplot中为每个x值在y轴上绘制平均值

[英]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.

相关问题 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? 如何使用pyplot旋转图的x轴和y轴 - How can I rotate a plot x axis and y axis using pyplot Pyplot 在热图上 Y 轴的指定值上绘制水平线 - Pyplot plot horizontal line on specified value of Y axis on Heatmap 绘制每个 x 值的 y 值的平均值 - Plot average of y values for every x value 如何通过使用pyplot降低y轴的值来对散点图plot进行排序? - How to sort scatter plot by decreasing values of y axis using pyplot? 如何在X轴上相对于Y值绘制日期和时间(Python) - How to plot date and time in X axis against Y value (Python) 当 x 和 y 轴共享时,如何为每个单独的 plot 子图添加 x 和 y 标签? - How to add x and y labels for each individual plot subplots when x and y axis is shared? 在x轴上绘制索引值并在y轴上绘制每个列值的频率的直方图 - Plot histogram with index values on x axis and frequencies of each column value on y axis 如何在 matplotlib.pyplot 中设置 X 和 Y 轴标题 - How to set X and Y axis Title in matplotlib.pyplot 如何使用y轴上的百分比和x轴上的每个指标绘制性能百分比? - How to plot performance percentages using percentage on y axis and each metric on x axis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM