【问题标题】:Legend Color and Plot Line Color Mismatch图例颜色和绘图线颜色不匹配
【发布时间】:2020-05-04 22:40:13
【问题描述】:

你好 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.

谁能帮我想出一种方法可以将图例的颜色与情节的颜色对齐?

提前感谢您!

【问题讨论】:

  • 如果你忽略了plt.legend() 电话,它不就行了吗?
  • 嗨,我需要自定义设置图例,因为我使用的是数据透视表,而另一列的“名称”会扭曲图例。我得到类似 ---revenue,Action

标签: python matplotlib legend mismatch


【解决方案1】:

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

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

plt.plot(x, y, label = 'Action')
plt.plot(x, y, label = 'Comedy')
.
.
.
plt.legend()
plt.show()

【讨论】:

  • 谢谢,这不是我想要的理想方式,但它确实有效。理想情况下,我的代码使用 pivot_table.plot() 会非常简洁。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多