Spines图

这个演示比较:

  • 正常轴,四边都有spine;
  • 仅在左侧和底部有spine的轴;
  • 使用自定义边界限制spine范围的轴。

Spines图示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. x = np.linspace(0, 2 * np.pi, 100)
  4. y = 2 * np.sin(x)
  5. fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)
  6. ax0.plot(x, y)
  7. ax0.set_title('normal spines')
  8. ax1.plot(x, y)
  9. ax1.set_title('bottom-left spines')
  10. # Hide the right and top spines
  11. ax1.spines['right'].set_visible(False)
  12. ax1.spines['top'].set_visible(False)
  13. # Only show ticks on the left and bottom spines
  14. ax1.yaxis.set_ticks_position('left')
  15. ax1.xaxis.set_ticks_position('bottom')
  16. ax2.plot(x, y)
  17. # Only draw spine between the y-ticks
  18. ax2.spines['left'].set_bounds(-1, 1)
  19. # Hide the right and top spines
  20. ax2.spines['right'].set_visible(False)
  21. ax2.spines['top'].set_visible(False)
  22. # Only show ticks on the left and bottom spines
  23. ax2.yaxis.set_ticks_position('left')
  24. ax2.xaxis.set_ticks_position('bottom')
  25. # Tweak spacing between subplots to prevent labels from overlapping
  26. plt.subplots_adjust(hspace=0.5)
  27. plt.show()

下载这个示例