python - matplotlib/python 中错误栏的边框

标签 python matplotlib

我正在寻找一种在绘图中的错误栏上设置黑色边框的方法,

以下代码:

ax.errorbar(x, y, yerr, fmt='o', label='label',color="#8da0cb",capthick=2, elinewidth=2,zorder=10)

产生:

errorbar

我发现如果错误栏周围有一个像标记上那样的黑色边框,会更美观。

感谢您提供的任何帮助

最佳答案

这不是一个很好的解决方案,但是您可以通过在原始误差条后面再次绘制误差条,使用更宽的线条和上限思考,并将这些误差条的颜色设置为黑色来接近。我们可以利用 zorder kwarg 将它们放在其他的后面。

这是 MWE:

import matplotlib.pyplot as plt
import numpy as np

# Fake data
x=np.arange(0,5,1)
y=np.ones(x.shape)
yerr = np.ones(x.shape)/4.

# Create figure
fig,ax = plt.subplots(1)

# Set some limits
ax.set_xlim(-1,5)
ax.set_ylim(-2,4)

# Plot errorbars with the line color you want
ax.errorbar(x,y,yerr, fmt='o',color='r',capthick=2,elinewidth=2,capsize=3,zorder=10)

# Plot black errorbars behind (lower zorder) with a wider line and cap thinkness
ax.errorbar(x,y,yerr, fmt='o',color='k',capthick=4,elinewidth=4,capsize=4,zorder=5)

plt.show()

enter image description here


同样,这不是一个完美的解决方案,但至少它允许您将其包含在图例中。这次,我们将使用matplotlib.patheffects,而不是绘制两次误差线。模块添加 Stroke到错误栏。

errorbar 返回多个 Line2DLineCollection 对象,因此我们需要将笔划应用于每个相关对象。

import matplotlib.patheffects as path_effects

e = ax.errorbar(x,y,yerr, fmt='o',color='r',capthick=2,elinewidth=2, label='path effects')

e[1][0].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
                          path_effects.Normal()])
e[1][1].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
                          path_effects.Normal()])
e[2][0].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
                          path_effects.Normal()])

ax.legend(loc=0)

enter image description here

关于python - matplotlib/python 中错误栏的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173774/

相关文章:

python - 凯拉斯图像数据生成器 : problem with data and label shape

python - 手动调用 __enter__ 和 __exit__

python - 在缺少公共(public) x 值的两条线之间填充

python - 我怎样才能在符号对数图中的刻度之间保持相同的长度?

python - modbus-tk 用于 Modbus RTU,读/写多个寄存器(fn 代码 23),返回异常代码 1

python - 如何在 python 中创建一个运算符函数?

python - matplotlib - 更改图大小但保持字体大小不变

python - 使用 pyinstaller 构建 python pylab/matplotlib exe

python - Python (Jython) 的第一步 : Importing

python - 减少绘图刻度数