误差条形图的不同方法

可以将错误指定为常数值(如errorbar_demo.py中所示)。但是,此示例通过指定错误值数组来演示它们的不同之处。

如果原始x和y数据的长度为N,则有两个选项:

  1. 数组形状为(N,): 每个点的误差都不同,但误差值是对称的(即,上下两个值相等)。
  2. 数组形状为(2, N): 每个点的误差不同,并且下限和上限(按该顺序)不同(非对称情况)。

此外,此示例演示如何使用带有误差线的对数刻度。

误差条形图的不同方法 - 图1

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # example data
  4. x = np.arange(0.1, 4, 0.5)
  5. y = np.exp(-x)
  6. # example error bar values that vary with x-position
  7. error = 0.1 + 0.2 * x
  8. fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
  9. ax0.errorbar(x, y, yerr=error, fmt='-o')
  10. ax0.set_title('variable, symmetric error')
  11. # error bar values w/ different -/+ errors that
  12. # also vary with the x-position
  13. lower_error = 0.4 * error
  14. upper_error = error
  15. asymmetric_error = [lower_error, upper_error]
  16. ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o')
  17. ax1.set_title('variable, asymmetric error')
  18. ax1.set_yscale('log')
  19. plt.show()

下载这个示例