图例颜色和图线颜色不匹配

问题描述 投票:0回答:1

Hello Overstack社区,

我是Python的初学者,我无法使绘图的颜色与绘图的颜色保持一致。

请在下面找到我编写的代码:-

merged_genres_mmd_plot.pivot_table(index='release_year', columns='genres', aggfunc='count').plot(figsize=(16,8))
plt.title('Popularity of Genre over the Years', fontsize=18)
plt.legend(['Action','Comedy','Drama','Horror','Western'])
plt.xlabel('Release Year',labelpad = 25)
plt.show()

This is the plot I am getting.

有人可以帮助我找出一种使图例的颜色与图的颜色对齐的方法吗?

预先感谢您!

python matplotlib legend mismatch
1个回答
0
投票

您的问题有几个答案,我认为最好的是:

使用您拥有的数据,您可以选择x和y轴是什么,使用pl.plot()您可以绘制所有具有的曲线并定义其标签,例如:

plt.plot(x,y,label ='Action')plt.plot(x,y,label ='喜剧')。。。plt.legend()plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.