我想更改错误栏颜色.经过多次尝试后,我找到了一条路:
a = plt.gca()
b = a.bar(range(4), [2]*4, yerr=range(4))
c = a.get_children()[8]
c.set_color(['r','r','b','r'])
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法?当然a.get_children()[8]不是一般的解决方案.

我创建了一个带有Pandas的条形图,其中我展示了一些国家的数量变化,并根据每个国家的大陆设置了条形颜色.我使用以下代码绘制图形.该代码基于此问题的第二个回复:
s = pd.Series(
listOfQuantities,
listOfCountiesNames
)
''' Assign color to each country based on the continent '''
colormapping = {'AF':'k','AS':'r','EU':'g','OC':'r','NA':'b','SA':'y'}
colorstring = ""
for country in listOfCountiesNames:
continent = countryToContinent[country]
colorstring += colormapping[continent]
pd.Series.plot(
s,
kind='bar',
color=colorstring,
grid=False,
)
Run Code Online (Sandbox Code Playgroud)
我想创建一个类似于我在附加图像中显示的图例(图例不是由python生成的,我手动添加).是否可以用熊猫绘制这样的自定义图例,还是可以实现与其他图形库类似的东西?此外,我也很欣赏有关此类数据的替代绘图类型的建议.

我有一个如下的条形图: http://matplotlib.org/examples/api/barchart_demo.html
在这种情况下,让我们假设G1-G5组中的每一组代表每组中男性参加某些考试的平均分数,并且每组中的女性参加相同的考试.
现在让我们说我有一些与每个组相关的其他功能(平均可爱性(在1-5之间浮动)).
Ex: Avg Likability of men in G1 - 1.33
Avg Likability of women in G1 - 4.6
Avg Likability of men in G2- 5.0
.... etc...
Run Code Online (Sandbox Code Playgroud)
让我们假设1 - 不讨人喜欢,5 - 非常讨人喜欢
我想知道如何通过改变颜色示例的阴影将这个可爱性特征结合到每个条形图中:因为上面例子中第1组的人有1.33,所以他们的图形将被涂上比红色阴影更浅的红色阴影. G2,因为G2的男性有5.0可爱,他们的酒吧将是图中最暗的红色,对女性来说也是如此.
我希望我已经说清楚了.如果有人能指出matplotlib中可以实现这一目标的资源,我真的很感激,因为我对这个软件包很新.
提前致谢.
我有以下pandas数据帧:
a b
bob 23 25
john 13 21
paul 20 19
david 17 14
michael 14 24
neil 22 11
df.plot(kind='barh')
Run Code Online (Sandbox Code Playgroud)
我使用了熊猫情节功能.我想制作一个带有不同颜色的所有行(名称)的条形图有没有办法做到这一点?
我尝试为以下条形图中的每个条形选择特定颜色:
鉴于我们有以下带有变量的 pandas 系列win_corr:
fruity -0.380938
hard -0.310382
pluribus -0.247448
nougat 0.199375
caramel 0.213416
crispedricewafer 0.324680
peanutyalmondy 0.406192
bar 0.429929
chocolate 0.636517
Name: winpercent, dtype: float64
Run Code Online (Sandbox Code Playgroud)
为了可视化,我使用以下代码:
fig, ax = plt.subplots(figsize=(8,6))
ax = sns.barplot(win_corr.values,win_corr.index, orient='h', color='deepskyblue')
ax.set_ylabel('')
ax.set_xlabel('Value')
plt.title('Correlation Coefficients for winpercent')
ax.bar_label(ax.containers[0], fmt= '%0.1f', label_type='center' )
#ax[0].set_color['r'] # did not work to change the color
plt.show()
Run Code Online (Sandbox Code Playgroud)
为了改变颜色,我尝试了 类似问题的以下解决方案:
color = ['black','red','green','orange','blue','limegreen','darkgreen','royalblue','navy']
fig, ax = plt.subplots(figsize=(8,6))
ax = sns.barplot(win_corr.values,win_corr.index, orient='h', color=color) # did NOT work
ax.set_ylabel('')
ax.set_xlabel('Value') …Run Code Online (Sandbox Code Playgroud)