test_table.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib.testing.decorators import image_comparison
  4. from matplotlib.table import CustomCell, Table
  5. from matplotlib.path import Path
  6. def test_non_square():
  7. # Check that creating a non-square table works
  8. cellcolors = ['b', 'r']
  9. plt.table(cellColours=cellcolors)
  10. @image_comparison(['table_zorder.png'], remove_text=True)
  11. def test_zorder():
  12. data = [[66386, 174296],
  13. [58230, 381139]]
  14. colLabels = ('Freeze', 'Wind')
  15. rowLabels = ['%d year' % x for x in (100, 50)]
  16. cellText = []
  17. yoff = np.zeros(len(colLabels))
  18. for row in reversed(data):
  19. yoff += row
  20. cellText.append(['%1.1f' % (x/1000.0) for x in yoff])
  21. t = np.linspace(0, 2*np.pi, 100)
  22. plt.plot(t, np.cos(t), lw=4, zorder=2)
  23. plt.table(cellText=cellText,
  24. rowLabels=rowLabels,
  25. colLabels=colLabels,
  26. loc='center',
  27. zorder=-2,
  28. )
  29. plt.table(cellText=cellText,
  30. rowLabels=rowLabels,
  31. colLabels=colLabels,
  32. loc='upper center',
  33. zorder=4,
  34. )
  35. plt.yticks([])
  36. @image_comparison(['table_labels.png'])
  37. def test_label_colours():
  38. dim = 3
  39. c = np.linspace(0, 1, dim)
  40. colours = plt.cm.RdYlGn(c)
  41. cellText = [['1'] * dim] * dim
  42. fig = plt.figure()
  43. ax1 = fig.add_subplot(4, 1, 1)
  44. ax1.axis('off')
  45. ax1.table(cellText=cellText,
  46. rowColours=colours,
  47. loc='best')
  48. ax2 = fig.add_subplot(4, 1, 2)
  49. ax2.axis('off')
  50. ax2.table(cellText=cellText,
  51. rowColours=colours,
  52. rowLabels=['Header'] * dim,
  53. loc='best')
  54. ax3 = fig.add_subplot(4, 1, 3)
  55. ax3.axis('off')
  56. ax3.table(cellText=cellText,
  57. colColours=colours,
  58. loc='best')
  59. ax4 = fig.add_subplot(4, 1, 4)
  60. ax4.axis('off')
  61. ax4.table(cellText=cellText,
  62. colColours=colours,
  63. colLabels=['Header'] * dim,
  64. loc='best')
  65. @image_comparison(['table_cell_manipulation.png'], remove_text=True)
  66. def test_diff_cell_table():
  67. cells = ('horizontal', 'vertical', 'open', 'closed', 'T', 'R', 'B', 'L')
  68. cellText = [['1'] * len(cells)] * 2
  69. colWidths = [0.1] * len(cells)
  70. _, axs = plt.subplots(nrows=len(cells), figsize=(4, len(cells)+1))
  71. for ax, cell in zip(axs, cells):
  72. ax.table(
  73. colWidths=colWidths,
  74. cellText=cellText,
  75. loc='center',
  76. edges=cell,
  77. )
  78. ax.axis('off')
  79. plt.tight_layout()
  80. def test_customcell():
  81. types = ('horizontal', 'vertical', 'open', 'closed', 'T', 'R', 'B', 'L')
  82. codes = (
  83. (Path.MOVETO, Path.LINETO, Path.MOVETO, Path.LINETO, Path.MOVETO),
  84. (Path.MOVETO, Path.MOVETO, Path.LINETO, Path.MOVETO, Path.LINETO),
  85. (Path.MOVETO, Path.MOVETO, Path.MOVETO, Path.MOVETO, Path.MOVETO),
  86. (Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY),
  87. (Path.MOVETO, Path.MOVETO, Path.MOVETO, Path.LINETO, Path.MOVETO),
  88. (Path.MOVETO, Path.MOVETO, Path.LINETO, Path.MOVETO, Path.MOVETO),
  89. (Path.MOVETO, Path.LINETO, Path.MOVETO, Path.MOVETO, Path.MOVETO),
  90. (Path.MOVETO, Path.MOVETO, Path.MOVETO, Path.MOVETO, Path.LINETO),
  91. )
  92. for t, c in zip(types, codes):
  93. cell = CustomCell((0, 0), visible_edges=t, width=1, height=1)
  94. code = tuple(s for _, s in cell.get_path().iter_segments())
  95. assert c == code
  96. @image_comparison(['table_auto_column.png'])
  97. def test_auto_column():
  98. fig = plt.figure()
  99. # iterable list input
  100. ax1 = fig.add_subplot(4, 1, 1)
  101. ax1.axis('off')
  102. tb1 = ax1.table(cellText=[['Fit Text', 2],
  103. ['very long long text, Longer text than default', 1]],
  104. rowLabels=["A", "B"],
  105. colLabels=["Col1", "Col2"],
  106. loc="center")
  107. tb1.auto_set_font_size(False)
  108. tb1.set_fontsize(12)
  109. tb1.auto_set_column_width([-1, 0, 1])
  110. # iterable tuple input
  111. ax2 = fig.add_subplot(4, 1, 2)
  112. ax2.axis('off')
  113. tb2 = ax2.table(cellText=[['Fit Text', 2],
  114. ['very long long text, Longer text than default', 1]],
  115. rowLabels=["A", "B"],
  116. colLabels=["Col1", "Col2"],
  117. loc="center")
  118. tb2.auto_set_font_size(False)
  119. tb2.set_fontsize(12)
  120. tb2.auto_set_column_width((-1, 0, 1))
  121. #3 single inputs
  122. ax3 = fig.add_subplot(4, 1, 3)
  123. ax3.axis('off')
  124. tb3 = ax3.table(cellText=[['Fit Text', 2],
  125. ['very long long text, Longer text than default', 1]],
  126. rowLabels=["A", "B"],
  127. colLabels=["Col1", "Col2"],
  128. loc="center")
  129. tb3.auto_set_font_size(False)
  130. tb3.set_fontsize(12)
  131. tb3.auto_set_column_width(-1)
  132. tb3.auto_set_column_width(0)
  133. tb3.auto_set_column_width(1)
  134. #4 non integer iterable input
  135. ax4 = fig.add_subplot(4, 1, 4)
  136. ax4.axis('off')
  137. tb4 = ax4.table(cellText=[['Fit Text', 2],
  138. ['very long long text, Longer text than default', 1]],
  139. rowLabels=["A", "B"],
  140. colLabels=["Col1", "Col2"],
  141. loc="center")
  142. tb4.auto_set_font_size(False)
  143. tb4.set_fontsize(12)
  144. tb4.auto_set_column_width("-101")
  145. def test_table_cells():
  146. fig, ax = plt.subplots()
  147. table = Table(ax)
  148. cell = table.add_cell(1, 2, 1, 1)
  149. assert isinstance(cell, CustomCell)
  150. assert cell is table[1, 2]
  151. cell2 = CustomCell((0, 0), 1, 2, visible_edges=None)
  152. table[2, 1] = cell2
  153. assert table[2, 1] is cell2
  154. # make sure gettitem support has not broken
  155. # properties and setp
  156. table.properties()
  157. plt.setp(table)