![](/img/trans.png)
[英]How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot?
[英]How to plot average in y axis for each x value in pyplot
我正在使用matplotlib.pyplot可視化我的數據。 在熊貓中,我有'hour'和'favourite_count'
。 hour的值形式為0到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()
通過在繪制之前添加以下行來執行平均步驟:
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軸現在是索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.