单选按钮

使用单选按钮选择绘图的属性。

单选按钮允许您在可视化中选择多个选项。在这种情况下,按钮允许用户选择要在图中显示的三种不同正弦波中的一种。

单选按钮示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.widgets import RadioButtons
  4. t = np.arange(0.0, 2.0, 0.01)
  5. s0 = np.sin(2*np.pi*t)
  6. s1 = np.sin(4*np.pi*t)
  7. s2 = np.sin(8*np.pi*t)
  8. fig, ax = plt.subplots()
  9. l, = ax.plot(t, s0, lw=2, color='red')
  10. plt.subplots_adjust(left=0.3)
  11. axcolor = 'lightgoldenrodyellow'
  12. rax = plt.axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
  13. radio = RadioButtons(rax, ('2 Hz', '4 Hz', '8 Hz'))
  14. def hzfunc(label):
  15. hzdict = {'2 Hz': s0, '4 Hz': s1, '8 Hz': s2}
  16. ydata = hzdict[label]
  17. l.set_ydata(ydata)
  18. plt.draw()
  19. radio.on_clicked(hzfunc)
  20. rax = plt.axes([0.05, 0.4, 0.15, 0.15], facecolor=axcolor)
  21. radio2 = RadioButtons(rax, ('red', 'blue', 'green'))
  22. def colorfunc(label):
  23. l.set_color(label)
  24. plt.draw()
  25. radio2.on_clicked(colorfunc)
  26. rax = plt.axes([0.05, 0.1, 0.15, 0.15], facecolor=axcolor)
  27. radio3 = RadioButtons(rax, ('-', '--', '-.', 'steps', ':'))
  28. def stylefunc(label):
  29. l.set_linestyle(label)
  30. plt.draw()
  31. radio3.on_clicked(stylefunc)
  32. plt.show()

下载这个示例