【发布时间】:2016-03-23 09:05:29
【问题描述】:
我正在寻找一种方法在我的情节中的错误栏上设置黑色边框,
以下代码:
ax.errorbar(x, y, yerr, fmt='o', label='label',color="#8da0cb",capthick=2, elinewidth=2,zorder=10)
产生:
如果错误栏周围有一个黑色边框,就像标记上的那样,我觉得更美观。
感谢您提供的任何帮助
【问题讨论】:
标签: python matplotlib
我正在寻找一种方法在我的情节中的错误栏上设置黑色边框,
以下代码:
ax.errorbar(x, y, yerr, fmt='o', label='label',color="#8da0cb",capthick=2, elinewidth=2,zorder=10)
产生:
如果错误栏周围有一个黑色边框,就像标记上的那样,我觉得更美观。
感谢您提供的任何帮助
【问题讨论】:
标签: python matplotlib
不是一个很好的解决方案,但您可以通过在原始错误栏后面再次绘制错误栏来接近,使用更宽的线条和上限,并将这些错误栏的颜色设置为黑色。我们可以利用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)
【讨论】:
capsize。不知道传说,抱歉
patheffects。我已经编辑了答案。它仍然不完美,但可能会给你一些东西。
据我在webpage of pyplot 中提供的信息所见,我没有看到您所要求的有效kwargs。
存在mfc, mec, ms 和mew,它们是markerfacecolor, markeredgecolor, markersize 和markeredgewith。它可能是asked in GitHub,以便人们考虑到这一点并将其添加到下一个版本的 matplotlib 中。
也看看the answer for this question asked in Stackoverflow,我不信能做到。
【讨论】: