【发布时间】:2020-09-08 08:25:12
【问题描述】:
我想知道如何在我的条形图中添加一条趋势线(或最佳拟合线)。我试过搜索,但发现自己很困惑。这是我用来创建条形图的代码:
dfY = pd.DataFrame(data={"Year":dataYVote['Year'], "Vote": dataYVote['Vote']})
year_list = []
avg_votes = []
for year in np.unique(dfY["Year"]):
year_list.append(year)
avg_votes.append(dfY.loc[dfY["Year"]==year, "Vote"].mean())
plt.bar(year_list, avg_votes, width = 0.5)
plt.xlabel('Year')
plt.ylabel('Amount of Votes')
plt.title('Average Amount of Votes for Each Year')
plt.show()
非常感谢任何帮助:)
【问题讨论】:
标签: python dataframe matplotlib trendline