Python 误差上限

Python 误差上限,python,matplotlib,errorbar,Python,Matplotlib,Errorbar,是否可能有一个上限(带向下箭头),该点以最佳值为中心,同时存在上限误差 大概是这样的: 我正在尝试: import numpy as np import matplotlib.pyplot as plt x = np.array([10, 15, 20, 25, 30, 35]) x_el = np.array([1, 1, 2, 25, 1, 2, 1]) x_eu = np.array([1, 1, 2, 1, 1, 2, 1]) y = np.array([29, 15, 9, 10,

是否可能有一个上限(带向下箭头),该点以最佳值为中心,同时存在上限误差

大概是这样的:

我正在尝试:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([10, 15, 20, 25, 30, 35])
x_el = np.array([1, 1, 2, 25, 1, 2, 1])
x_eu = np.array([1, 1, 2, 1, 1, 2, 1])
y = np.array([29, 15, 9, 10, 25, 14])
y_el = np.array([1, 1, 2, 1, 1, 2, 1])
y_eu = np.array([11,1,2,1,1,2,1])

fig, ax = plt.subplots()

for i in range(len(x)):
    if (x[i] - x_el[i]) == 0:
        el = 0
        ax.errorbar(x[i], y[i], yerr=[[y_el[i]], [y_eu[i]]], xerr=[[el],[x_eu[i]]],
                    c='b', capsize=2, elinewidth=1, marker='o',
                    xuplims=True)
    else:
        ax.errorbar(x[i], y[i], yerr=[[y_el[i]], [y_eu[i]]], xerr=[[x_el[i]], [x_eu[i]]],
                    c='b', capsize=2, elinewidth=1, marker='o')
但结果是:


点编号4既没有上限误差,也没有上限误差。

简短回答是肯定的,但必须分别绘制上限和误差条。让我们从正确绘制法线错误条开始。如果您的数据已经在numpy数组中,则可以在不循环的情况下执行此操作:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([10, 15, 20, 25, 30, 35])
x_el = np.array([1, 1, 2, 25, 1, 2])
x_eu = np.array([1, 1, 2, 1, 1, 2])
y = np.array([29, 15, 9, 10, 25, 14])
y_el = np.array([1, 1, 2, 1, 1, 2])
y_eu = np.array([11, 1, 2, 1, 1, 2])

fig, ax = plt.subplots()

mask = (x != x_el)

ax.errorbar(x, y, yerr=[y_el, y_eu], xerr=[x_el * mask, x_eu],
            c='b', capsize=2, elinewidth=1, marker='o', linestyle='none')
请注意,我将错误条数组裁剪为与
x
相同的大小,这允许我使用
计算掩码=操作员。因为您对除了
x_el
中的错误条之外的所有错误条都感兴趣,所以我乘以掩码。掩码是一个布尔值,任何被屏蔽的错误条都将被设置为零。此时将正确绘制所有其他条形图:

现在,您可以使用相同的遮罩(但反转)绘制上限:

ax.errorbar(x[~mask], y[~mask], xerr=x_el[~mask],
            c='b', capsize=2, elinewidth=1, marker='o', linestyle='none',
            xuplims=True)
结果是

如果您对一个延伸至零的长箭头不感兴趣,您可以将其缩短至任意大小:

ax.errorbar(x[~mask], y[~mask], xerr=1,
            c='b', capsize=2, elinewidth=1, marker='o', linestyle='none',
            xuplims=True)

备选方案

由于
xuplims
接受布尔数组,您甚至可以通过一个绘图调用获得非常接近的结果。但是,只要是真的,就会消除右栏:

mask = (x == x_el)
ax.errorbar(x, y, yerr=[y_el, y_eu], xerr=[x_el, x_eu],
            c='b', capsize=2, elinewidth=1, marker='o', linestyle='none',
            xuplims=mask)

在这种情况下,您必须填写正确的栏:

ax.errorbar(x[mask], y[mask], xerr=[np.zeros_like(x_eu)[mask], x_eu[mask]],
            c='b', capsize=2, elinewidth=1, marker='o', linestyle='none')

是的。。。我正在尝试使用“ax.errorbar()”,但我没有找到解决方案……这要好得多。请将您的尝试编辑到问题中,并解释结果与期望结果的差异。@Mad物理学家,很抱歉,现在我已经编辑了。如果(x[I]-x_el[I])==0:
,为什么要检查
?你把错误栏关掉了。我想我明白你想做什么。一旦我离开手机,几个小时后我会发布一些东西。