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

San*_*nez 59 python matplotlib bar-chart pandas

假设我的条形图如下:

BarPlot

关于如何为每个载体设置不同颜色的任何想法?例如,AK是Red,GA是Green等等?

我在Python中使用Pandas和matplotlib

>>> 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.
Run Code Online (Sandbox Code Playgroud)

对于上面的建议,我们如何精确地枚举ax.get_children()并检查对象类型是否为矩形?所以如果对象是矩形,我们会分配不同的随机颜色?

CT *_*Zhu 103

简单,只需使用 .set_color

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

在此输入图像描述

对于你的新问题,也没有那么难,只需要从轴上找到条形图,例如:

>>> 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.
Run Code Online (Sandbox Code Playgroud)

如果您有一个复杂的图并想要首先识别条形,请添加:

>>> 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>]
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这将删除栏的黑色边框(边缘).它可以通过使用:`barlist [0] .set_edgecolor("black")`来恢复.更好的方法可能是使用`barlist [0] .set_facecolor('r')`,它不会改变条形的边缘. (6认同)

7st*_*tud 34

我假设您正在使用Series.plot()来绘制数据.如果你在这里查看Series.plot()的文档:

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series.plot.html

没有列出颜色参数,您可以在其中设置条形图的颜色.

但是,Series.plot()文档在参数列表的末尾声明了以下内容:

kwds : keywords
Options to pass to matplotlib plotting method
Run Code Online (Sandbox Code Playgroud)

这意味着当你将Series.plot()的kind参数指定为bar时,Series.plot()实际上会调用matplotlib.pyplot.bar(),而matplotlib.pyplot.bar()将被发送给所有额外的您在Series.plot()的参数列表末尾指定的关键字参数.

如果您在此处检查matplotlib.pyplot.bar()方法的文档:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar

..it也接受参数列表末尾的关键字参数,如果仔细阅读已识别参数名称列表,其中一个是颜色,可以是指定条形图不同颜色的序列.

总而言之,如果在Series.plot()参数列表的末尾指定color关键字参数,关键字参数将被中继到matplotlib.pyplot.bar()方法.这是证明:

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series(
    [5, 4, 4, 1, 12],
    index = ["AK", "AX", "GA", "SQ", "WN"]
)

#Set descriptions:
plt.title("Total Delay Incident Caused by Carrier")
plt.ylabel('Delay Incident')
plt.xlabel('Carrier')

#Set tick colors:
ax = plt.gca()
ax.tick_params(axis='x', colors='blue')
ax.tick_params(axis='y', colors='red')

#Plot the data:
my_colors = 'rgbkymc'  #red, green, blue, black, etc.

pd.Series.plot(
    s, 
    kind='bar', 
    color=my_colors,
)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,如果序列中的条形比颜色多,则颜色将重复.

  • 我必须在 pandas: 0.24.2, matplotlib: 3.1.0 中使用 `color = list('rgbkymc')` (5认同)

Yan*_* P. 12

更新大熊猫0.17.0

@ 7stud对最新熊猫版本的回答需要打电话

s.plot( 
    kind='bar', 
    color=my_colors,
)
Run Code Online (Sandbox Code Playgroud)

代替

pd.Series.plot(
    s, 
    kind='bar', 
    color=my_colors,
)
Run Code Online (Sandbox Code Playgroud)

绘图函数已成为Series,DataFrame对象的成员,实际上pd.Series.plot使用color参数调用会产生错误

  • 我必须在 pandas: 0.24.2, matplotlib: 3.1.0 中使用 `color = list('rgbkymc')` (8认同)