为matplotlib中的每个plt.plot添加平均线



我正在寻求帮助,为我的线图中的每个plt.plot添加一条水平线。每个plt.plot=《宋飞正传》的一季。y轴=IMDB评分,x轴=《宋飞正传》的集数。

为了澄清,我想画几条不同的水平线,给出不同y值的平均值。

输出是此处的附加图像这是我迄今为止的代码:

# sets size for figure.
plt.figure(dpi=300)
# so you don't get 'ax' is not defined when running
ax = plt.gca()
# Put each season's IMDB rating into a variable for easy referencing
S1imdb = season_1['imdbRating'] 
S2imdb = season_2['imdbRating']
S3imdb = season_3['imdbRating']
S4imdb = season_4['imdbRating']
S5imdb = season_5['imdbRating']
S6imdb = season_6['imdbRating']
S7imdb = season_7['imdbRating']
S8imdb = season_8['imdbRating']
S9imdb = season_9['imdbRating']

# plot each season's IMDB rating separately 
plt.plot(S1imdb, label='Season 1')
plt.plot(S2imdb, label='Season 2')
plt.plot(S3imdb, label='Season 3')
plt.plot(S4imdb, label='Season 4')
plt.plot(S5imdb, label='Season 5')
plt.plot(S6imdb, label='Season 6')
plt.plot(S7imdb, label='Season 7')
plt.plot(S8imdb, label='Season 8')
plt.plot(S9imdb, label='Season 9')

# title the X and Y axes
# x axis is the episode number
# y axis is the rating from IMDB x/10
plt.xlabel('Episode Number')
plt.ylabel('IMDB Rating')
# legend
plt.legend(loc=2, prop={'size': 5})
# print out the plot
plt.show()

明白了!我刚刚使用了ax.hlines,并根据季节的平均值手动编辑了y轴上的平均线的长度,根据每个季节的索引ax.hline(7.700000,0,6,线宽=1,颜色='k'(编辑了x轴上的长度

最新更新