误差条形图中的上限和下限

在matplotlib中,误差条可以有“限制”。对误差线应用限制实质上使误差单向。因此,可以分别通过uplimslolimsxuplimsxlolims参数在y方向和x方向上应用上限和下限。 这些参数可以是标量或布尔数组。

例如,如果xlolimsTrue,则x-error条形将仅从数据扩展到递增值。如果uplims是一个填充了False的数组,除了第4和第7个值之外,所有y误差条都是双向的,除了第4和第7个条形,它们将从数据延伸到减小的y值。

条形图限制示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # example data
  4. x = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0])
  5. y = np.exp(-x)
  6. xerr = 0.1
  7. yerr = 0.2
  8. # lower & upper limits of the error
  9. lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
  10. uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
  11. ls = 'dotted'
  12. fig, ax = plt.subplots(figsize=(7, 4))
  13. # standard error bars
  14. ax.errorbar(x, y, xerr=xerr, yerr=yerr, linestyle=ls)
  15. # including upper limits
  16. ax.errorbar(x, y + 0.5, xerr=xerr, yerr=yerr, uplims=uplims,
  17. linestyle=ls)
  18. # including lower limits
  19. ax.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims,
  20. linestyle=ls)
  21. # including upper and lower limits
  22. ax.errorbar(x, y + 1.5, xerr=xerr, yerr=yerr,
  23. lolims=lolims, uplims=uplims,
  24. marker='o', markersize=8,
  25. linestyle=ls)
  26. # Plot a series with lower and upper limits in both x & y
  27. # constant x-error with varying y-error
  28. xerr = 0.2
  29. yerr = np.zeros_like(x) + 0.2
  30. yerr[[3, 6]] = 0.3
  31. # mock up some limits by modifying previous data
  32. xlolims = lolims
  33. xuplims = uplims
  34. lolims = np.zeros(x.shape)
  35. uplims = np.zeros(x.shape)
  36. lolims[[6]] = True # only limited at this index
  37. uplims[[3]] = True # only limited at this index
  38. # do the plotting
  39. ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
  40. xlolims=xlolims, xuplims=xuplims,
  41. uplims=uplims, lolims=lolims,
  42. marker='o', markersize=8,
  43. linestyle='none')
  44. # tidy up the figure
  45. ax.set_xlim((0, 5.5))
  46. ax.set_title('Errorbar upper and lower limits')
  47. plt.show()

下载这个示例