开发者

Multi-Coloured bars in matplotlib

开发者 https://www.devze.com 2023-04-09 17:43 出处:网络
I have a graph in matplotlib (that displays correc开发者_StackOverflow中文版tly), but I was hoping to have a different colour for each bar (still the same bar graph though). Is this possible?

I have a graph in matplotlib (that displays correc开发者_StackOverflow中文版tly), but I was hoping to have a different colour for each bar (still the same bar graph though). Is this possible?

Thanks


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)

Multi-Coloured bars in matplotlib

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号