自定义spine边界

使用自定义边界来限制脊椎范围的spine演示。

自定义spine边界示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. x = np.linspace(0, 2*np.pi, 50)
  6. y = np.sin(x)
  7. y2 = y + 0.1 * np.random.normal(size=x.shape)
  8. fig, ax = plt.subplots()
  9. ax.plot(x, y, 'k--')
  10. ax.plot(x, y2, 'ro')
  11. # set ticks and tick labels
  12. ax.set_xlim((0, 2*np.pi))
  13. ax.set_xticks([0, np.pi, 2*np.pi])
  14. ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$'])
  15. ax.set_ylim((-1.5, 1.5))
  16. ax.set_yticks([-1, 0, 1])
  17. # Only draw spine between the y-ticks
  18. ax.spines['left'].set_bounds(-1, 1)
  19. # Hide the right and top spines
  20. ax.spines['right'].set_visible(False)
  21. ax.spines['top'].set_visible(False)
  22. # Only show ticks on the left and bottom spines
  23. ax.yaxis.set_ticks_position('left')
  24. ax.xaxis.set_ticks_position('bottom')
  25. plt.show()

下载这个示例