gpt4 book ai didi

python - 仅在 matplotlib 上移除底部错误上限

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:31 25 4
gpt4 key购买 nike

我只想显示一半的误差线,因为它们是对称的;因为我不知道如何用“干净的方式”做到这一点,所以我选择在底部使用 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 .如您所见,我绘制半误差条的方法可能不是应该做的。

那么有什么方法可以隐藏底线或绘制半误差线的更好方法吗?

最佳答案

ax.errorbar可以选择设置 uplims=Truelolims=True 来表示均值分别代表上限或下限。不幸的是,您似乎无法将这些选项直接与 ax.bar 一起使用,因此我们必须分别绘制误差条和条形图。

ax.errorbaruplims/lolims 选项的文档:

lolims / uplims / xlolims / xuplims : bool, optional, default:None

These arguments can be used to indicate that a value gives only upper/lower limits. In that case a caret symbol is used to indicate this. lims-arguments may be of the same type as xerr and yerr. To use limits with inverted axes, set_xlim() or set_ylim() must be called before errorbar().

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

您可以在 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

如果您不喜欢箭头,可以通过更改从 ax.errorbar< 返回(作为第二项)的 caplines 的标记将它们更改为扁平大写字母。我们可以将它们从箭头更改为标记样式 _,然后通过 .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

关于python - 仅在 matplotlib 上移除底部错误上限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45752981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com