Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Different Bar color in matplotlib Python [duplicate]

Supposely, I have the bar chart as below:

BarPlot

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?

like image 558
Santiago Munez Avatar asked Sep 24 '13 05:09

Santiago Munez


People also ask

How do you make each bar a different color in matplotlib?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.

Is it possible to have a separate color for each category in a bar graph Python?

there is no color parameter listed where you might be able to set the colors for your bar graph.

How do I change the color of individual bars?

On a chart, select the individual data marker that you want to change. On the Format tab, in the Shape Styles group, click Shape Fill. Do one of the following: To use a different fill color, under Theme Colors or Standard Colors, click the color that you want to use.


1 Answers

Simple, just use .set_color

>>> barlist=plt.bar([1,2,3,4], [1,2,3,4]) >>> barlist[0].set_color('r') >>> plt.show() 

enter image description here

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>] 
like image 93
CT Zhu Avatar answered Oct 16 '22 03:10

CT Zhu