Guest User

Untitled

a guest
Oct 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.widgets import CheckButtons,AxesWidget
  4.  
  5. class PremiumCheckButtons(CheckButtons,AxesWidget):
  6. def __init__(self, ax, labels, actives, linecolor="k", showedge=True, **kw):
  7. AxesWidget.__init__(self, ax)
  8.  
  9. ax.set_xticks([])
  10. ax.set_yticks([])
  11. ax.set_navigate(False)
  12. if not showedge:
  13. ax.axis("off")
  14. linekw = {'solid_capstyle': 'butt', "color" : linecolor}
  15. class Handler(object):
  16. def legend_artist(self, legend, orig_handle, fontsize, handlebox):
  17. x0, y0 = handlebox.xdescent, handlebox.ydescent
  18. height = handlebox.height
  19. self.line1 = plt.Line2D([x0,x0+height],[y0,y0+height], **linekw)
  20. self.line2 = plt.Line2D([x0,x0+height],[y0+height,y0], **linekw)
  21. self.rect = plt.Rectangle((x0,y0),height, height,
  22. edgecolor="k", fill=False)
  23. handlebox.add_artist(self.rect)
  24. handlebox.add_artist(self.line1)
  25. handlebox.add_artist(self.line2)
  26. return [self.line1, self.line2, self.rect]
  27.  
  28. self.box = ax.legend(handles = [object() for i in labels ],
  29. labels = labels,
  30. handler_map={object: Handler()}, **kw)
  31.  
  32. self.lines = [(h[0],h[1]) for h in self.box.legendHandles]
  33. self.rectangles = [h[2] for h in self.box.legendHandles]
  34. self.labels = self.box.texts
  35.  
  36. for i,(l1,l2) in enumerate(self.lines):
  37. l1.set_visible(actives[i])
  38. l2.set_visible(actives[i])
  39.  
  40. self.connect_event('button_press_event', self._clicked)
  41.  
  42. self.cnt = 0
  43. self.observers = {}
  44.  
  45.  
  46. t = np.arange(0.0, 2.0, 0.01)
  47. s0 = np.sin(2*np.pi*t)
  48. s1 = np.sin(4*np.pi*t)
  49. s2 = np.sin(6*np.pi*t)
  50.  
  51. fig, (rax,ax) = plt.subplots(nrows=2, gridspec_kw=dict(height_ratios = [0.1,1]) )
  52. l0, = ax.plot(t, s0, visible=False, lw=2)
  53. l1, = ax.plot(t, s1, lw=2)
  54. l2, = ax.plot(t, s2, lw=2)
  55. plt.subplots_adjust(left=0.2)
  56.  
  57. check = PremiumCheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (False, True, True),
  58. showedge = False, ncol=3)
  59.  
  60.  
  61. def func(label):
  62. if label == '2 Hz':
  63. l0.set_visible(not l0.get_visible())
  64. elif label == '4 Hz':
  65. l1.set_visible(not l1.get_visible())
  66. elif label == '6 Hz':
  67. l2.set_visible(not l2.get_visible())
  68. fig.canvas.draw_idle()
  69. check.on_clicked(func)
  70.  
  71. plt.show()
Add Comment
Please, Sign In to add comment