首页 > 解决方案 > 如何修复 Python 条形图中的图例颜色问题?

问题描述

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

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

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()

当我运行代码时,有些颜色是相同的。

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

这是屏幕截图 在此处输入图像描述

标签: pythondataframeplotcolors

解决方案


我的答案:

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

更改为代码

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

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

推荐阅读