python - 如何修复 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()
当我运行代码时,有些颜色是相同的。
如何在图例部分定义独特的颜色?
解决方案
我的答案:
在我定义colormap
为彩虹之后,图例部分中所有定义的颜色都变得独一无二。
更改为代码
ax = df.plot(kind='bar', stacked=True,figsize=(13,10))
到
ax = df.plot(kind='bar', stacked=True,figsize=(13,10), colormap='rainbow')
推荐阅读
- r - glmnet 在使用预测时产生错误
- python - 为什么 LinearRegression() 预测的结果是荒谬的?
- c# - ASP.NET MVC单元测试WEB API Post方法:Request.RequestUri抛出异常
- kubernetes - Kubernetes weave how to create pod with network that uses bridge as network?
- sql - 在 postgresql ubuntu 中找不到数据库文件
- entity-framework-core - AutoMapper - 将源对象映射为目标对象中的属性
- javascript - Angular 中的路由服务
- git - 打开拉取请求但如何应用公关提交?
- javascript - 不出现字符串的正则表达式匹配
- python - 如何在 Python 中定义模型中类的属性(需要更新)?