备注
前往结尾 下载完整示例代码。
指定误差线的不同方式#
错误可以指定为一个常数值(如 Errorbar 函数 所示)。然而,这个示例展示了如何通过指定错误值数组来使它们变化。
如果原始的 x 和 y 数据的长度为 N,有两种选择:
- 形状为 (N,) 的数组:
每个点的误差不同,但误差值是对称的(即下限和上限值相等)。
- 形状为 (2, N) 的数组:
每个点的误差不同,上下限(按此顺序)也不同(非对称情况)
此外,此示例展示了如何使用带有误差线的对数刻度。
import matplotlib.pyplot as plt
import numpy as np
# example data
x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
# example error bar values that vary with x-position
error = 0.1 + 0.2 * x
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
ax0.errorbar(x, y, yerr=error, fmt='-o')
ax0.set_title('variable, symmetric error')
# error bar values w/ different -/+ errors that
# also vary with the x-position
lower_error = 0.4 * error
upper_error = error
asymmetric_error = [lower_error, upper_error]
ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o')
ax1.set_title('variable, asymmetric error')
ax1.set_yscale('log')
plt.show()