我正在寻找一种在绘图中的错误栏上设置黑色边框的方法,
以下代码:
ax.errorbar(x, y, yerr, fmt='o', label='label',color="#8da0cb",capthick=2, elinewidth=2,zorder=10)
产生:
我发现如果错误栏周围有一个像标记上那样的黑色边框,会更美观。
感谢您提供的任何帮助
最佳答案
这不是一个很好的解决方案,但是您可以通过在原始误差条后面再次绘制误差条,使用更宽的线条和上限思考,并将这些误差条的颜色设置为黑色来接近。我们可以利用 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()
同样,这不是一个完美的解决方案,但至少它允许您将其包含在图例中。这次,我们将使用matplotlib.patheffects
,而不是绘制两次误差线。模块添加 Stroke
到错误栏。
errorbar
返回多个 Line2D
和 LineCollection
对象,因此我们需要将笔划应用于每个相关对象。
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)
关于python - matplotlib/python 中错误栏的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173774/