【问题标题】:Border on errorbars in matplotlib/pythonmatplotlib/python 中错误栏的边框
【发布时间】: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


    【解决方案1】:

    不是一个很好的解决方案,但您可以通过在原始错误栏后面再次绘制错误栏来接近,使用更宽的线条和上限,并将这些错误栏的颜色设置为黑色。我们可以利用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 返回多个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)
    

    【讨论】:

    • 我还会添加 'capsize = x' 以使帽子的侧面也显示为黑色
    • 知道如何通过它以使其也出现在图例中吗?
    • 好主意回复:capsize。不知道传说,抱歉
    • @AnthonyLethuillier 好的,所以我有一个想法——我们可以使用patheffects。我已经编辑了答案。它仍然不完美,但可能会给你一些东西。
    • 太好了,我现在用它,我在 Gihub 上发布了一个问题,我们将看看它的结果。
    【解决方案2】:

    据我在webpage of pyplot 中提供的信息所见,我没有看到您所要求的有效kwargs

    存在mfc, mec, msmew,它们是markerfacecolor, markeredgecolor, markersizemarkeredgewith。它可能是asked in GitHub,以便人们考虑到这一点并将其添加到下一个版本的 matplotlib 中。

    也看看the answer for this question asked in Stackoverflow,我不信能做到。

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 2014-08-01
      • 1970-01-01
      • 2011-02-19
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      相关资源
      最近更新 更多