test_table.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(
  103. cellText=[['Fit Text', 2],
  104. ['very long long text, Longer text than default', 1]],
  105. rowLabels=["A", "B"],
  106. colLabels=["Col1", "Col2"],
  107. loc="center")
  108. tb1.auto_set_font_size(False)
  109. tb1.set_fontsize(12)
  110. tb1.auto_set_column_width([-1, 0, 1])
  111. # iterable tuple input
  112. ax2 = fig.add_subplot(4, 1, 2)
  113. ax2.axis('off')
  114. tb2 = ax2.table(
  115. cellText=[['Fit Text', 2],
  116. ['very long long text, Longer text than default', 1]],
  117. rowLabels=["A", "B"],
  118. colLabels=["Col1", "Col2"],
  119. loc="center")
  120. tb2.auto_set_font_size(False)
  121. tb2.set_fontsize(12)
  122. tb2.auto_set_column_width((-1, 0, 1))
  123. #3 single inputs
  124. ax3 = fig.add_subplot(4, 1, 3)
  125. ax3.axis('off')
  126. tb3 = ax3.table(
  127. cellText=[['Fit Text', 2],
  128. ['very long long text, Longer text than default', 1]],
  129. rowLabels=["A", "B"],
  130. colLabels=["Col1", "Col2"],
  131. loc="center")
  132. tb3.auto_set_font_size(False)
  133. tb3.set_fontsize(12)
  134. tb3.auto_set_column_width(-1)
  135. tb3.auto_set_column_width(0)
  136. tb3.auto_set_column_width(1)
  137. #4 non integer iterable input
  138. ax4 = fig.add_subplot(4, 1, 4)
  139. ax4.axis('off')
  140. tb4 = ax4.table(
  141. cellText=[['Fit Text', 2],
  142. ['very long long text, Longer text than default', 1]],
  143. rowLabels=["A", "B"],
  144. colLabels=["Col1", "Col2"],
  145. loc="center")
  146. tb4.auto_set_font_size(False)
  147. tb4.set_fontsize(12)
  148. tb4.auto_set_column_width("-101")
  149. def test_table_cells():
  150. fig, ax = plt.subplots()
  151. table = Table(ax)
  152. cell = table.add_cell(1, 2, 1, 1)
  153. assert isinstance(cell, CustomCell)
  154. assert cell is table[1, 2]
  155. cell2 = CustomCell((0, 0), 1, 2, visible_edges=None)
  156. table[2, 1] = cell2
  157. assert table[2, 1] is cell2
  158. # make sure gettitem support has not broken
  159. # properties and setp
  160. table.properties()
  161. plt.setp(table)