【问题标题】:How can I fix legend color issue in Bar graph in Python?如何修复 Python 条形图中的图例颜色问题?
【发布时间】:2020-11-18 16:27:37
【问题描述】:

我在条形图的图例部分定义许多颜色时遇到问题。

在完成一些基本过程后,我使用下面显示的代码绘制了一个图形。

ax = df.plot(kind='bar', stacked=True,figsize=(13,10))
plt.title('Title List', fontsize=20) 
leg = ax.legend(loc='center right', bbox_to_anchor=(1.3, 0.5), ncol=1)
plt.tight_layout()
plt.savefig('images/image1.png', bbox_inches = "tight")  
plt.show()

当我运行代码时,有些颜色是一样的。

如何在图例部分定义独特的颜色?

这是截图

【问题讨论】:

  • 请附上你的输出截图
  • @Divyessh Maheshwari 我附上了图例截图的颜色。
  • 所有电源颜色都被占用了。
  • 主要有10种颜色
  • @Divyessh Maheshwari 如何将它们定义为独特的

标签: python dataframe plot colors


【解决方案1】:

我的回答:

在我将colormap 定义为彩虹之后,图例部分中所有定义的颜色都变得唯一。

更改代码

ax = df.plot(kind='bar', stacked=True,figsize=(13,10))

ax = df.plot(kind='bar', stacked=True,figsize=(13,10), colormap='rainbow')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2018-12-14
    • 2020-10-07
    • 2020-02-20
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多