user972094
user972094

Reputation: 11

Multi-Coloured bars in matplotlib

I have a graph in matplotlib (that displays correctly), but I was hoping to have a different colour for each bar (still the same bar graph though). Is this possible?

Thanks

Upvotes: 1

Views: 672

Answers (1)

joaquin
joaquin

Reputation: 85603

If you color at graph creation time:

In [15]: x= range(5)
In [16]: y = [10, 23, 12, 45, 32]
In [17]: color = ['r', 'b', 'y', 'g', 'c']
In [18]: lines = bar(x, y, color=color)

enter image description here

If you want to change color of first bar after graph creation, then note that you got a list of your bars in lines:

In [19]: lines      
Out[19]:
[<matplotlib.patches.Rectangle object at 0x02
 <matplotlib.patches.Rectangle object at 0x02
 <matplotlib.patches.Rectangle object at 0x02
 <matplotlib.patches.Rectangle object at 0x02
 <matplotlib.patches.Rectangle object at 0x02

Then, just simply set its color:

In [20]: lines[0].set_color('c')    #changes from original red to cyan

Upvotes: 2

Related Questions