test_axislines.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.testing.decorators import image_comparison
  4. from matplotlib.transforms import IdentityTransform
  5. from mpl_toolkits.axisartist.axislines import AxesZero, SubplotZero, Subplot
  6. from mpl_toolkits.axisartist import Axes, SubplotHost
  7. @image_comparison(['SubplotZero.png'], style='default')
  8. def test_SubplotZero():
  9. # Remove this line when this test image is regenerated.
  10. plt.rcParams['text.kerning_factor'] = 6
  11. fig = plt.figure()
  12. ax = SubplotZero(fig, 1, 1, 1)
  13. fig.add_subplot(ax)
  14. ax.axis["xzero"].set_visible(True)
  15. ax.axis["xzero"].label.set_text("Axis Zero")
  16. for n in ["top", "right"]:
  17. ax.axis[n].set_visible(False)
  18. xx = np.arange(0, 2 * np.pi, 0.01)
  19. ax.plot(xx, np.sin(xx))
  20. ax.set_ylabel("Test")
  21. @image_comparison(['Subplot.png'], style='default')
  22. def test_Subplot():
  23. # Remove this line when this test image is regenerated.
  24. plt.rcParams['text.kerning_factor'] = 6
  25. fig = plt.figure()
  26. ax = Subplot(fig, 1, 1, 1)
  27. fig.add_subplot(ax)
  28. xx = np.arange(0, 2 * np.pi, 0.01)
  29. ax.plot(xx, np.sin(xx))
  30. ax.set_ylabel("Test")
  31. ax.axis["top"].major_ticks.set_tick_out(True)
  32. ax.axis["bottom"].major_ticks.set_tick_out(True)
  33. ax.axis["bottom"].set_label("Tk0")
  34. def test_Axes():
  35. fig = plt.figure()
  36. ax = Axes(fig, [0.15, 0.1, 0.65, 0.8])
  37. fig.add_axes(ax)
  38. ax.plot([1, 2, 3], [0, 1, 2])
  39. ax.set_xscale('log')
  40. fig.canvas.draw()
  41. @image_comparison(['ParasiteAxesAuxTrans_meshplot.png'],
  42. remove_text=True, style='default', tol=0.075)
  43. def test_ParasiteAxesAuxTrans():
  44. data = np.ones((6, 6))
  45. data[2, 2] = 2
  46. data[0, :] = 0
  47. data[-2, :] = 0
  48. data[:, 0] = 0
  49. data[:, -2] = 0
  50. x = np.arange(6)
  51. y = np.arange(6)
  52. xx, yy = np.meshgrid(x, y)
  53. funcnames = ['pcolor', 'pcolormesh', 'contourf']
  54. fig = plt.figure()
  55. for i, name in enumerate(funcnames):
  56. ax1 = SubplotHost(fig, 1, 3, i+1)
  57. fig.add_subplot(ax1)
  58. ax2 = ax1.get_aux_axes(IdentityTransform(), viewlim_mode=None)
  59. if name.startswith('pcolor'):
  60. getattr(ax2, name)(xx, yy, data[:-1, :-1])
  61. else:
  62. getattr(ax2, name)(xx, yy, data)
  63. ax1.set_xlim((0, 5))
  64. ax1.set_ylim((0, 5))
  65. ax2.contour(xx, yy, data, colors='k')
  66. @image_comparison(['axisline_style.png'], remove_text=True, style='mpl20')
  67. def test_axisline_style():
  68. fig = plt.figure(figsize=(2, 2))
  69. ax = fig.add_subplot(axes_class=AxesZero)
  70. ax.axis["xzero"].set_axisline_style("-|>")
  71. ax.axis["xzero"].set_visible(True)
  72. ax.axis["yzero"].set_axisline_style("->")
  73. ax.axis["yzero"].set_visible(True)
  74. for direction in ("left", "right", "bottom", "top"):
  75. ax.axis[direction].set_visible(False)
  76. @image_comparison(['axisline_style_size_color.png'], remove_text=True,
  77. style='mpl20')
  78. def test_axisline_style_size_color():
  79. fig = plt.figure(figsize=(2, 2))
  80. ax = fig.add_subplot(axes_class=AxesZero)
  81. ax.axis["xzero"].set_axisline_style("-|>", size=2.0, facecolor='r')
  82. ax.axis["xzero"].set_visible(True)
  83. ax.axis["yzero"].set_axisline_style("->, size=1.5")
  84. ax.axis["yzero"].set_visible(True)
  85. for direction in ("left", "right", "bottom", "top"):
  86. ax.axis[direction].set_visible(False)
  87. @image_comparison(['axisline_style_tight.png'], remove_text=True,
  88. style='mpl20')
  89. def test_axisline_style_tight():
  90. fig = plt.figure(figsize=(2, 2), layout='tight')
  91. ax = fig.add_subplot(axes_class=AxesZero)
  92. ax.axis["xzero"].set_axisline_style("-|>", size=5, facecolor='g')
  93. ax.axis["xzero"].set_visible(True)
  94. ax.axis["yzero"].set_axisline_style("->, size=8")
  95. ax.axis["yzero"].set_visible(True)
  96. for direction in ("left", "right", "bottom", "top"):
  97. ax.axis[direction].set_visible(False)
  98. @image_comparison(['subplotzero_ylabel.png'], style='mpl20')
  99. def test_subplotzero_ylabel():
  100. fig = plt.figure()
  101. ax = fig.add_subplot(111, axes_class=SubplotZero)
  102. ax.set(xlim=(-3, 7), ylim=(-3, 7), xlabel="x", ylabel="y")
  103. zero_axis = ax.axis["xzero", "yzero"]
  104. zero_axis.set_visible(True) # they are hidden by default
  105. ax.axis["left", "right", "bottom", "top"].set_visible(False)
  106. zero_axis.set_axisline_style("->")