水平条形图

这个例子展示了一个简单的水平条形图。

水平条形图示;

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. plt.rcdefaults()
  6. fig, ax = plt.subplots()
  7. # Example data
  8. people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
  9. y_pos = np.arange(len(people))
  10. performance = 3 + 10 * np.random.rand(len(people))
  11. error = np.random.rand(len(people))
  12. ax.barh(y_pos, performance, xerr=error, align='center',
  13. color='green', ecolor='black')
  14. ax.set_yticks(y_pos)
  15. ax.set_yticklabels(people)
  16. ax.invert_yaxis() # labels read top-to-bottom
  17. ax.set_xlabel('Performance')
  18. ax.set_title('How fast do you want to go today?')
  19. plt.show()

下载这个示例