仅在matplotlib上删除底部错误上限

时间:2017-08-18 09:16:59

标签: python python-3.x matplotlib

我想只显示半个误差条,因为它们是对称的;因为我不知道如何以“干净的方式”做到这一点,我选择使用底部为0的不对称误差;但是,当我显示上限时,我意识到这不是最好的方法。

以下是代码:

  import numpy as np
  import matplotlib.pyplot as plt

  N = 5
  men_means = (20, 35, 30, 35, 27)
  men_std = (2, 3, 4, 1, 2)

  ind = np.arange(N)
  width = 0.35

  fig, ax = plt.subplots()
  rects1 = ax.bar(ind, men_means, width, color='r',yerr=[np.zeros(len(men_std)),men_std],capsize = 5)

  women_means = (25, 32, 34, 20, 25)
  women_std = (3, 5, 2, 3, 3)
  rects2 = ax.bar(ind + width, women_means, width, color='y',yerr=[np.zeros(len(women_std)),women_std],capsize = 5)

  plt.show()

这是我得到的情节:myPlot。正如你所看到的,我绘制半误差条的方式可能不是应该做的。

那么有没有办法隐藏底帽线或更好的方法来绘制半误差条?

2 个答案:

答案 0 :(得分:3)

ax.errorbar可以选择设置uplims=Truelolims=True来表示该方法分别重复上限或下限。不幸的是,您似乎无法直接在ax.bar使用这些选项,因此我们必须分别绘制错误栏和条形图。

uplims/lolimsax.errorbar选项的文档:

  

lolims / uplims / xlolims / xuplims:bool,可选,默认:无

     

这些参数可用于指示值仅给出上限/下限。在这种情况下,使用插入符号来表示这一点。 lims-arguments可以与xerryerr的类型相同。要使用反转轴限制,必须在errorbar()之前调用set_xlim()set_ylim()

请注意,使用此选项会将大写字母更改为箭头。如果您需要平顶帽而不是箭头,请参阅下面的示例,了解如何将它们更改回大写字母。

您可以在this example on the matplotlib website中看到这些选项。

现在,这是你的例子,修改过:

import numpy as np
import matplotlib.pyplot as plt

N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)

ind = np.arange(N)
width = 0.35

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r')
err1 = ax.errorbar(ind, men_means, yerr=men_std, lolims=True, capsize = 0, ls='None', color='k')

women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y')
err2 = ax.errorbar(ind + width, women_means, yerr=women_std, lolims=True, capsize = 0, ls='None', color='k')

plt.show()

enter image description here

如果您不喜欢箭头,可以通过更改caplines返回的ax.errorbar标记(作为第二项)将其更改为平顶。我们可以将它们从箭头更改为标记样式_,然后使用.set_markersize控制它们的大小:

import numpy as np
import matplotlib.pyplot as plt

N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)

ind = np.arange(N)
width = 0.35

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r')
plotline1, caplines1, barlinecols1 = ax.errorbar(
        ind, men_means, yerr=men_std, lolims=True,
        capsize = 0, ls='None', color='k')

caplines1[0].set_marker('_')
caplines1[0].set_markersize(20)

women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, color='y')
plotline2, caplines2, barlinecols2 = ax.errorbar(
        ind + width, women_means, yerr=women_std,
        lolims=True, capsize = 0, ls='None', color='k')

caplines2[0].set_marker('_')
caplines2[0].set_markersize(10)

plt.show()

enter image description here

答案 1 :(得分:0)

非常感谢, 根据您的回答,以下方法是我的解决方案:

def process_error_bar(ax, x, y, y_err, marker_size):
    """
    hide half error_bar
    :param ax: plt.subplots()
    :param x: x position
    :param y: y position
    :param y_err: y errors
    :param marker_size: size
    """

    lolims = []
    uplims = []
    for y_value in y:
        if y_value < 0:
            lolims.append(False)
            uplims.append(True)
        else:
            lolims.append(True)
            uplims.append(False)

    plotline, caplines, barlinecols = ax.errorbar(
        x, y, yerr=y_err, lolims=lolims, uplims=uplims,
        capsize=0, ls='None', color='k')

    # [arrow] -> [-]
    for capline in caplines:
        capline.set_marker('_')
        capline.set_markersize(marker_size)

enter image description here