Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot average in y axis for each x value in pyplot

I am using matplotlib.pyplot to visualize my data. In pandas I have columns 'hour' and 'favourite_count'. hour has values form 0 to 24. favourite_count is a continuous variable. What I want is to plot a bar chart which visualizes the average favourite_count for each hour. 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). How can I plot a graph that visualizes hour vs 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()
like image 558
Kabilesh Avatar asked Oct 27 '25 06:10

Kabilesh


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!