我正在寻找帮助,以便在我的线图中为每个plt.plot添加一条水平线,以该特定plt.plot的平均值为准。每个plot.plot = Seinfeld和一个季节。 y轴= IMDB评分,x轴=塞因菲尔德集数。
为了明确起见,我在给定不同y值的平均值的同时绘制了几条不同的水平线。
输出为附件图像here 到目前为止,这是我的代码:
# 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()
答案 0 :(得分:0)
知道了!我只是使用ax.hlines并根据季节的平均值手动编辑y轴上平均线的长度,并根据每个季节的索引ax.hlines(7.700000,0,6,linewidth = 1,颜色='k')