Python 在 matplotlib 的条形图中设置不同的误差条颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21912197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Different error bar colors in bar plot in matplotlib
提问by colinfang
Following Setting Different Bar color in matplotlib Python
在 matplotlib Python 中设置不同的条形颜色之后
I would like to change the error bar colors. I have figured out a way after a number of attempts:
我想更改误差条颜色。经过多次尝试,我找到了一种方法:
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'])
Is there any better way? Certainly a.get_children()[8]is not a general solution at all.
有没有更好的办法?当然,这a.get_children()[8]根本不是一个通用的解决方案。


采纳答案by Joe Kington
If you just want to set them to a single color, use the error_kwkwarg (expected to be a dict of keyword arguments that's passed on to ax.errorbar).
如果您只想将它们设置为单一颜色,请使用error_kwkwarg(预计是传递给 的关键字参数的字典ax.errorbar)。
Also, just so you know, you can pass a sequence of facecolors directly to bar, though this won't change the errorbar color.
此外,正如您所知,您可以将一系列 facecolors 直接传递给bar,尽管这不会改变错误栏的颜色。
As a quick example:
举个简单的例子:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.bar(range(4), [2] * 4, yerr=range(1, 5), alpha=0.5,
color=['red', 'green', 'blue', 'cyan', 'magenta'],
error_kw=dict(ecolor='gray', lw=2, capsize=5, capthick=2))
ax.margins(0.05)
plt.show()


However, if you want the errorbars to be different colors, you'll either need to plot them individually or modify them afterwards.
但是,如果您希望误差条具有不同的颜色,则需要单独绘制它们或在之后修改它们。
If you use the latter option, the capline colors actually can't be changed individually (note that they're not changed in @falsetru's example either). For example:
如果您使用后一个选项,实际上不能单独更改帽线颜色(请注意,它们在@falsetru 的示例中也没有更改)。例如:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
colors = ['red', 'green', 'blue', 'cyan', 'magenta']
container = ax.bar(range(4), [2] * 4, yerr=range(1, 5), alpha=0.5, color=colors,
error_kw=dict(lw=2, capsize=5, capthick=2))
ax.margins(0.05)
connector, caplines, (vertical_lines,) = container.errorbar.lines
vertical_lines.set_color(colors)
plt.show()


The caplinesobject in the answer above is a tuple of two Line2Ds: One line for all of the top caps, and one line for all of the bottom caps. There's not way to change the colors of the caps individually (it's easy to set them all to the same color) without removing that artist and creating a LineCollectionin its place.
caplines上面答案中的对象是一个由两个Line2Ds 组成的元组:一行用于所有顶盖,一行用于所有底盖。如果不删除该艺术家并LineCollection在其位置创建一个,则无法单独更改上限的颜色(很容易将它们设置为相同的颜色)。
Therefore, you're better off just plotting the errorbars individually in this case.
因此,在这种情况下,最好单独绘制误差线。
E.g.
例如
import matplotlib.pyplot as plt
x, height, error = range(4), [2] * 4, range(1,5)
colors = ['red', 'green', 'blue', 'cyan', 'magenta']
fig, ax = plt.subplots()
ax.bar(x, height, alpha=0.5, color=colors)
ax.margins(0.05)
for pos, y, err, color in zip(x, height, error, colors):
ax.errorbar(pos + 0.4, y, err, lw=2, capsize=5, capthick=2, color=color)
plt.show()


回答by falsetru
Not a general solution neither, but here it is.
也不是一般的解决方案,但它是。
a = plt.gca()
b = a.bar(range(4), [2]*4, yerr=range(4))
c = b.errorbar.lines[2][b.errorbar.has_xerr] # <----
c.set_color(['r', 'r', 'b', 'r'])
# from matplotlib.collections import LineCollection
# next(i
# for i, x in enumerate(b.errorbar.lines)
# if x and any(isinstance(y, LineCollection) for y in x)) == 2

