跨度选择器

SpanSelector是一个鼠标小部件,用于选择xmin / xmax范围并绘制下轴中所选区域的详细视图

跨度选择器示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.widgets import SpanSelector
  4. # Fixing random state for reproducibility
  5. np.random.seed(19680801)
  6. fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))
  7. ax1.set(facecolor='#FFFFCC')
  8. x = np.arange(0.0, 5.0, 0.01)
  9. y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))
  10. ax1.plot(x, y, '-')
  11. ax1.set_ylim(-2, 2)
  12. ax1.set_title('Press left mouse button and drag to test')
  13. ax2.set(facecolor='#FFFFCC')
  14. line2, = ax2.plot(x, y, '-')
  15. def onselect(xmin, xmax):
  16. indmin, indmax = np.searchsorted(x, (xmin, xmax))
  17. indmax = min(len(x) - 1, indmax)
  18. thisx = x[indmin:indmax]
  19. thisy = y[indmin:indmax]
  20. line2.set_data(thisx, thisy)
  21. ax2.set_xlim(thisx[0], thisx[-1])
  22. ax2.set_ylim(thisy.min(), thisy.max())
  23. fig.canvas.draw()
  24. # Set useblit=True on most backends for enhanced performance.
  25. span = SpanSelector(ax1, onselect, 'horizontal', useblit=True,
  26. rectprops=dict(alpha=0.5, facecolor='red'))
  27. plt.show()

下载这个示例