表格演示

演示表函数以在图表中显示表格。

表格演示示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. data = [[ 66386, 174296, 75131, 577908, 32015],
  4. [ 58230, 381139, 78045, 99308, 160454],
  5. [ 89135, 80552, 152558, 497981, 603535],
  6. [ 78415, 81858, 150656, 193263, 69638],
  7. [139361, 331509, 343164, 781380, 52269]]
  8. columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
  9. rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]
  10. values = np.arange(0, 2500, 500)
  11. value_increment = 1000
  12. # Get some pastel shades for the colors
  13. colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
  14. n_rows = len(data)
  15. index = np.arange(len(columns)) + 0.3
  16. bar_width = 0.4
  17. # Initialize the vertical-offset for the stacked bar chart.
  18. y_offset = np.zeros(len(columns))
  19. # Plot bars and create text labels for the table
  20. cell_text = []
  21. for row in range(n_rows):
  22. plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
  23. y_offset = y_offset + data[row]
  24. cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
  25. # Reverse colors and text labels to display the last value at the top.
  26. colors = colors[::-1]
  27. cell_text.reverse()
  28. # Add a table at the bottom of the axes
  29. the_table = plt.table(cellText=cell_text,
  30. rowLabels=rows,
  31. rowColours=colors,
  32. colLabels=columns,
  33. loc='bottom')
  34. # Adjust layout to make room for the table:
  35. plt.subplots_adjust(left=0.2, bottom=0.2)
  36. plt.ylabel("Loss in ${0}'s".format(value_increment))
  37. plt.yticks(values * value_increment, ['%d' % val for val in values])
  38. plt.xticks([])
  39. plt.title('Loss by Disaster')
  40. plt.show()

下载这个示例