python条形图颜色设置_在matplotlib Python中设置不同的条形颜色

本文介绍了如何在使用Pandas和Matplotlib的Python环境中为条形图的每个条目设置不同的颜色。通过调用`ax.get_children()`并设置`.set_color()`属性,可以单独改变条形的颜色。示例代码展示了如何为条形图中的第一个条目设置红色,并提供了在复杂图表中查找并修改颜色的方法。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

Supposely, I have the bar chart as below:

wy1lP.png

Any ideas on how to set different colors for each carrier? As for example, AK would be Red, GA would be Green, etc?

I am using Pandas and matplotlib in Python

>>> f=plt.figure()

>>> ax=f.add_subplot(1,1,1)

>>> ax.bar([1,2,3,4], [1,2,3,4])

>>> ax.get_children()

[, , , , , , , , , , , ]

>>> ax.get_children()[2].set_color('r') #You can also try to locate the first patches.Rectangle object instead of direct calling the index.

For the suggestions above, how do exactly we could enumerate ax.get_children() and check if the object type is rectangle? So if the object is rectangle, we would assign different ra

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值