在matplotlib的条形图中设置不同的错误条颜色 [英] Setting Different error bar colors in bar plot in matplotlib

查看:569
本文介绍了在matplotlib的条形图中设置不同的错误条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循在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'])

还有更好的方法吗?当然a.get_children()[8]根本不是通用解决方案.

Is there any better way? Certainly a.get_children()[8] is not a general solution at all.

推荐答案

如果只想将它们设置为单一颜色,请使用error_kw kwarg(预期是传递给).

If you just want to set them to a single color, use the error_kw kwarg (expected to be a dict of keyword arguments that's passed on to ax.errorbar).

此外,您知道,您可以将一系列Facecolor直接传递给bar,尽管这不会更改错误栏的颜色.

Also, just so you know, you can pass a sequence of facecolors directly to bar, though this won't change the errorbar color.

作为一个简单的例子:

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.

如果使用后一个选项,则实际上不能单独更改标题颜色(请注意,@ falsetru的示例中也未更改).例如:

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:

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()

以上答案中的caplines对象是两个Line2D的元组:一行用于所有顶盖,而一行用于所有底盖.在不删除该歌手并在其位置上创建LineCollection的情况下,无法单独更改其颜色(很容易将它们全部设置为相同的颜色).

The caplines object 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 LineCollection in its place.

因此,在这种情况下,最好单独绘制误差线.

Therefore, you're better off just plotting the errorbars individually in this case.

例如

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()

这篇关于在matplotlib的条形图中设置不同的错误条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆