MatPlotLib中的非对称误差条

4

我有一个对数对数图,想要在6个数据点中的一个数据点上绘制正向误差线。其余的可以有正负误差。我该如何处理?

通常情况下,我是这样绘制误差线的:

plt.loglog(vsini_rand, vsini_rand_lit, 'bo', label='Randich+1996')
plt.errorbar(vsini_rand, vsini_rand_lit, xerr = sig_rand, color = 'gray', fmt='.', zorder=1)
plt.loglog(x,y,'r-', zorder=3, label='1:1')

1
请参阅文档:http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.errorbar - tacaswell
2个回答

3

阅读plt.errorbar的文档,如果想要绘制不对称误差线,需要使用参数xerr,其形状为2xN的序列。这样做会在数据相对于-row1和+row2处绘制误差线。如果你只想为一个点绘制正误差线,则应将下限定义为零。也就是说,如果你的数据是:

[x1, x2, ... , xn]

你需要提供序列:

[x0-,x0+,x1-,x1+, ... , xn-,xn+] 

作为xerr的参数。 希望这有帮助。

0
以下是如何在matplotlib中绘制不对称误差条的示例。即使使用对数-对数比例尺,您也可以使用此功能。
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)

def generate_data(num_points, num_repetitions):
    n, reps = num_points, num_repetitions
    # Generate fake data
    x = np.linspace(0, 4*np.pi, n)
    ys = np.array([
        np.sin(x) + np.random.normal(0, scale=0.3, size=n) for _ in range(reps)
    ])

    yavg = np.mean(ys, axis=0)
    ymins = np.min(ys, axis=0)
    ymaxs = np.max(ys, axis=0)
    yerr = [
        np.abs(yavg-ymins), # lower error
        np.abs(yavg-ymaxs)  # upper error 
    ]
    return x, yavg, ymins, ymaxs, yerr

def format_ax(axes, x):
    for ax in axes:
        ax.set_xlim(min(x), max(x))
        ax.set_xticks([])
        ax.set_yticks([])

        
def make_plot():
    fig, axes = plt.subplots(1,2, figsize=(8, 3))

    x, yavg, ymins, ymaxs, yerr = generate_data(50, 3)
    axes[0].errorbar(x, yavg, yerr=yerr, c='tab:orange',  elinewidth=0.75, marker='.', linestyle='none')

    x, yavg, ymins, ymaxs, yerr = generate_data(100, 15)
    axes[1].plot(x, ymins, ls="--", c='tab:orange', alpha=0.4)
    axes[1].plot(x, ymaxs, ls="--", c='tab:orange', alpha=0.4)
    axes[1].errorbar(x, yavg, yerr=yerr, c='tab:orange', alpha=0.2, lw=0.75, linestyle='none')
    axes[1].plot(x, yavg, c='tab:orange')

    format_ax(axes, x)
    axes[0].set_title("Example 1")
    axes[1].set_title("Example 2")
    plt.show()

make_plot()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接