与单位组合的条形图

此示例与以厘米为单位的条形图演示相同。

此示例需要 basic_units.py

与单位组合的条形图示例

  1. import numpy as np
  2. from basic_units import cm, inch
  3. import matplotlib.pyplot as plt
  4. N = 5
  5. menMeans = (150*cm, 160*cm, 146*cm, 172*cm, 155*cm)
  6. menStd = (20*cm, 30*cm, 32*cm, 10*cm, 20*cm)
  7. fig, ax = plt.subplots()
  8. ind = np.arange(N) # the x locations for the groups
  9. width = 0.35 # the width of the bars
  10. p1 = ax.bar(ind, menMeans, width, color='r', bottom=0*cm, yerr=menStd)
  11. womenMeans = (145*cm, 149*cm, 172*cm, 165*cm, 200*cm)
  12. womenStd = (30*cm, 25*cm, 20*cm, 31*cm, 22*cm)
  13. p2 = ax.bar(ind + width, womenMeans, width,
  14. color='y', bottom=0*cm, yerr=womenStd)
  15. ax.set_title('Scores by group and gender')
  16. ax.set_xticks(ind + width / 2)
  17. ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
  18. ax.legend((p1[0], p2[0]), ('Men', 'Women'))
  19. ax.yaxis.set_units(inch)
  20. ax.autoscale_view()
  21. plt.show()

下载这个示例