test_spines.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.testing.decorators import check_figures_equal, image_comparison
  4. @image_comparison(['spines_axes_positions'])
  5. def test_spines_axes_positions():
  6. # SF bug 2852168
  7. fig = plt.figure()
  8. x = np.linspace(0, 2*np.pi, 100)
  9. y = 2*np.sin(x)
  10. ax = fig.add_subplot(1, 1, 1)
  11. ax.set_title('centered spines')
  12. ax.plot(x, y)
  13. ax.spines['right'].set_position(('axes', 0.1))
  14. ax.yaxis.set_ticks_position('right')
  15. ax.spines['top'].set_position(('axes', 0.25))
  16. ax.xaxis.set_ticks_position('top')
  17. ax.spines['left'].set_color('none')
  18. ax.spines['bottom'].set_color('none')
  19. @image_comparison(['spines_data_positions'])
  20. def test_spines_data_positions():
  21. fig = plt.figure()
  22. ax = fig.add_subplot(1, 1, 1)
  23. ax.spines['left'].set_position(('data', -1.5))
  24. ax.spines['top'].set_position(('data', 0.5))
  25. ax.spines['right'].set_position(('data', -0.5))
  26. ax.spines['bottom'].set_position('zero')
  27. ax.set_xlim([-2, 2])
  28. ax.set_ylim([-2, 2])
  29. @check_figures_equal(extensions=["png"])
  30. def test_spine_nonlinear_data_positions(fig_test, fig_ref):
  31. plt.style.use("default")
  32. ax = fig_test.add_subplot()
  33. ax.set(xscale="log", xlim=(.1, 1))
  34. # Use position="data" to visually swap the left and right spines, using
  35. # linewidth to distinguish them. The calls to tick_params removes labels
  36. # (for image comparison purposes) and harmonizes tick positions with the
  37. # reference).
  38. ax.spines["left"].set_position(("data", 1))
  39. ax.spines["left"].set_linewidth(2)
  40. ax.spines["right"].set_position(("data", .1))
  41. ax.tick_params(axis="y", labelleft=False, direction="in")
  42. ax = fig_ref.add_subplot()
  43. ax.set(xscale="log", xlim=(.1, 1))
  44. ax.spines["right"].set_linewidth(2)
  45. ax.tick_params(axis="y", labelleft=False, left=False, right=True)
  46. @image_comparison(['spines_capstyle'])
  47. def test_spines_capstyle():
  48. # issue 2542
  49. plt.rc('axes', linewidth=20)
  50. fig = plt.figure()
  51. ax = fig.add_subplot(1, 1, 1)
  52. ax.set_xticks([])
  53. ax.set_yticks([])
  54. def test_label_without_ticks():
  55. fig = plt.figure()
  56. ax = fig.add_subplot(1, 1, 1)
  57. plt.subplots_adjust(left=0.3, bottom=0.3)
  58. ax.plot(np.arange(10))
  59. ax.yaxis.set_ticks_position('left')
  60. ax.spines['left'].set_position(('outward', 30))
  61. ax.spines['right'].set_visible(False)
  62. ax.set_ylabel('y label')
  63. ax.xaxis.set_ticks_position('bottom')
  64. ax.spines['bottom'].set_position(('outward', 30))
  65. ax.spines['top'].set_visible(False)
  66. ax.set_xlabel('x label')
  67. ax.xaxis.set_ticks([])
  68. ax.yaxis.set_ticks([])
  69. plt.draw()
  70. spine = ax.spines['left']
  71. spinebbox = spine.get_transform().transform_path(
  72. spine.get_path()).get_extents()
  73. assert ax.yaxis.label.get_position()[0] < spinebbox.xmin, \
  74. "Y-Axis label not left of the spine"
  75. spine = ax.spines['bottom']
  76. spinebbox = spine.get_transform().transform_path(
  77. spine.get_path()).get_extents()
  78. assert ax.xaxis.label.get_position()[1] < spinebbox.ymin, \
  79. "X-Axis label not below the spine"