在 matplotlib Python 中设置不同的条形颜色 [英] Setting Different Bar color in matplotlib Python
问题描述
假设,我有如下条形图:
关于如何为每个运营商设置不同颜色的任何想法?例如,AK 是红色,GA 是绿色,等等?
我在 Python 中使用 Pandas 和 matplotlib
<预><代码>>>>f=plt.figure()>>>ax=f.add_subplot(1,1,1)>>>ax.bar([1,2,3,4], [1,2,3,4])<4位艺术家的容器对象>>>>ax.get_children()[<matplotlib.axis.XAxis 对象在 0x6529850>,<matplotlib.axis.YAxis 对象在 0x78460d0>,<matplotlib.patches.Rectangle 对象在 0x733cc50>,<matplotlib.patches.Rectangle 对象,<matplotlib.patches.Rectangle3<x0d3<;matplotlib.patches.Rectangle 对象在 0x777f290>、<matplotlib.patches.Rectangle 对象在 0x777f710>、<matplotlib.text.Text 对象在 0x7836450>、<matplotlib.patches.Rectangle 对象<matplotlib.patches.Rectangle 对象在 0x777f710>.spines.Spine 对象位于 0x6529950>、对于上面的建议,我们究竟如何枚举 ax.get_children() 并检查对象类型是否为矩形?那么如果对象是矩形,我们会分配不同的随机颜色吗?
简单,使用.set_color
对于你的新问题,也不难,只需要从你的轴上找到条形,一个例子:
<预><代码>>>>f=plt.figure()>>>ax=f.add_subplot(1,1,1)>>>ax.bar([1,2,3,4], [1,2,3,4])<4位艺术家的容器对象>>>>ax.get_children()[<matplotlib.axis.XAxis 对象在 0x6529850>,<matplotlib.axis.YAxis 对象在 0x78460d0>,<matplotlib.patches.Rectangle 对象在 0x733cc50>,<matplotlib.patches.Rectangle 对象在 0x733cdd0>,<matplotlib.patches.Rectangle 对象在 0x777f290>,<matplotlib.patches.Rectangle 对象在 0x777f710>,<matplotlib.text.Text 对象在 0x7836450>,<matplotlib.patches.Rectangle 对象在 0x7836390>,<matplotlib.spines.Spine 对象在 0x6529950>,<matplotlib.spines.Spine 对象在 0x69aef50>,<matplotlib.spines.Spine 对象在 0x69ae310>,<matplotlib.spines.Spine 对象在 0x69aea50>]>>>ax.get_children()[2].set_color('r')#你也可以尝试定位第一个patch.Rectangle对象#而不是直接调用索引.如果您有一个复杂的图并想先识别条形,请添加这些条形:
<预><代码>>>>导入 matplotlib>>>childrenLS=ax.get_children()>>>barlist=filter(lambda x: isinstance(x, matplotlib.patches.Rectangle), childrenLS)[<matplotlib.patches.Rectangle 对象在 0x3103650>,<matplotlib.patches.Rectangle 对象在 0x3103810>,<matplotlib.patches.Rectangle 对象在 0x3129850>,<matplotlib.patches.Rectangle 对象在 0x3129cd0>,<matplotlib.patches.Rectangle 对象在 0x3112ad0>]Supposely, I have the bar chart as below:
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])
<Container object of 4 artists>
>>> ax.get_children()
[<matplotlib.axis.XAxis object at 0x6529850>, <matplotlib.axis.YAxis object at 0x78460d0>, <matplotlib.patches.Rectangle object at 0x733cc50>, <matplotlib.patches.Rectangle object at 0x733cdd0>, <matplotlib.patches.Rectangle object at 0x777f290>, <matplotlib.patches.Rectangle object at 0x777f710>, <matplotlib.text.Text object at 0x7836450>, <matplotlib.patches.Rectangle object at 0x7836390>, <matplotlib.spines.Spine object at 0x6529950>, <matplotlib.spines.Spine object at 0x69aef50>, <matplotlib.spines.Spine object at 0x69ae310>, <matplotlib.spines.Spine object at 0x69aea50>]
>>> 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 random color?
Simple, just use .set_color
>>> barlist=plt.bar([1,2,3,4], [1,2,3,4])
>>> barlist[0].set_color('r')
>>> plt.show()
For your new question, not much harder either, just need to find the bar from your axis, an example:
>>> f=plt.figure()
>>> ax=f.add_subplot(1,1,1)
>>> ax.bar([1,2,3,4], [1,2,3,4])
<Container object of 4 artists>
>>> ax.get_children()
[<matplotlib.axis.XAxis object at 0x6529850>,
<matplotlib.axis.YAxis object at 0x78460d0>,
<matplotlib.patches.Rectangle object at 0x733cc50>,
<matplotlib.patches.Rectangle object at 0x733cdd0>,
<matplotlib.patches.Rectangle object at 0x777f290>,
<matplotlib.patches.Rectangle object at 0x777f710>,
<matplotlib.text.Text object at 0x7836450>,
<matplotlib.patches.Rectangle object at 0x7836390>,
<matplotlib.spines.Spine object at 0x6529950>,
<matplotlib.spines.Spine object at 0x69aef50>,
<matplotlib.spines.Spine object at 0x69ae310>,
<matplotlib.spines.Spine object at 0x69aea50>]
>>> ax.get_children()[2].set_color('r')
#You can also try to locate the first patches.Rectangle object
#instead of direct calling the index.
If you have a complex plot and want to identify the bars first, add those:
>>> import matplotlib
>>> childrenLS=ax.get_children()
>>> barlist=filter(lambda x: isinstance(x, matplotlib.patches.Rectangle), childrenLS)
[<matplotlib.patches.Rectangle object at 0x3103650>,
<matplotlib.patches.Rectangle object at 0x3103810>,
<matplotlib.patches.Rectangle object at 0x3129850>,
<matplotlib.patches.Rectangle object at 0x3129cd0>,
<matplotlib.patches.Rectangle object at 0x3112ad0>]
这篇关于在 matplotlib Python 中设置不同的条形颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!