堆积条形图

这是使用 bar 创建带有误差线的堆积条形图的示例。注意yerr用于误差条的参数,并且底部用于将女人的条形堆叠在男人条形的顶部。

堆积条形图示;

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. N = 5
  4. menMeans = (20, 35, 30, 35, 27)
  5. womenMeans = (25, 32, 34, 20, 25)
  6. menStd = (2, 3, 4, 1, 2)
  7. womenStd = (3, 5, 2, 3, 3)
  8. ind = np.arange(N) # the x locations for the groups
  9. width = 0.35 # the width of the bars: can also be len(x) sequence
  10. p1 = plt.bar(ind, menMeans, width, yerr=menStd)
  11. p2 = plt.bar(ind, womenMeans, width,
  12. bottom=menMeans, yerr=womenStd)
  13. plt.ylabel('Scores')
  14. plt.title('Scores by group and gender')
  15. plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
  16. plt.yticks(np.arange(0, 81, 10))
  17. plt.legend((p1[0], p2[0]), ('Men', 'Women'))
  18. plt.show()

下载这个示例