Guest User

Untitled

a guest
Nov 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.lines as lines
  3. import numpy as np
  4.  
  5. class draggable_lines:
  6.  
  7. def __init__(self, ax, kind, XorY):
  8.  
  9. self.ax = ax
  10. self.c = ax.get_figure().canvas
  11. self.o = kind
  12. self.XorY = XorY
  13.  
  14. if kind == "h":
  15. x = [-1, 1]
  16. y = [XorY, XorY]
  17.  
  18. elif kind == "v":
  19. x = [XorY, XorY]
  20. y = [-1, 1]
  21.  
  22. else:
  23. print("choose h or v line")
  24.  
  25. self.line = lines.Line2D(x, y, picker=5)
  26. self.ax.add_line(self.line)
  27. self.c.draw()
  28. sid = self.c.mpl_connect('pick_event', self.clickonline)
  29.  
  30. # pick line when I select it
  31. def clickonline(self, event):
  32.  
  33. self.active_line = event.artist
  34. print("line selected ", event.artist)
  35. self.follower = self.c.mpl_connect("motion_notify_event", self.followmouse)
  36. self.releaser = self.c.mpl_connect("button_press_event", self.releaseonclick)
  37.  
  38. # The selected line must follow the mouse
  39. def followmouse(self, event):
  40.  
  41. if self.o == "h":
  42. self.line.set_ydata([event.ydata, event.ydata])
  43. else:
  44. self.line.set_xdata([event.xdata, event.xdata])
  45.  
  46. self.c.draw()
  47.  
  48. # release line on click
  49. def releaseonclick(self, event):
  50.  
  51. if self.o == "h":
  52. self.XorY = self.line.get_ydata()[0]
  53. else:
  54. self.XorY = self.line.get_xdata()[0]
  55.  
  56. print (self.XorY)
  57.  
  58. self.c.mpl_disconnect(self.releaser)
  59. self.c.mpl_disconnect(self.follower)
  60.  
  61.  
  62. plt.ion()
  63. fig = plt.figure()
  64. ax = fig.add_subplot(111)
  65. Vline = draggable_lines(ax, "h", 0.5)
  66. Tline = draggable_lines(ax, "v", 0.5)
  67. Tline2 = draggable_lines(ax, "v", 0.1)
  68.  
  69. if event.artist == self.line:
  70. # register other callbacks
  71.  
  72. import matplotlib.pyplot as plt
  73. import matplotlib.lines as lines
  74.  
  75. class draggable_lines:
  76. def __init__(self, ax, kind, XorY):
  77. self.ax = ax
  78. self.c = ax.get_figure().canvas
  79. self.o = kind
  80. self.XorY = XorY
  81.  
  82. if kind == "h":
  83. x = [-1, 1]
  84. y = [XorY, XorY]
  85.  
  86. elif kind == "v":
  87. x = [XorY, XorY]
  88. y = [-1, 1]
  89. self.line = lines.Line2D(x, y, picker=5)
  90. self.ax.add_line(self.line)
  91. self.c.draw_idle()
  92. self.sid = self.c.mpl_connect('pick_event', self.clickonline)
  93.  
  94. def clickonline(self, event):
  95. if event.artist == self.line:
  96. print("line selected ", event.artist)
  97. self.follower = self.c.mpl_connect("motion_notify_event", self.followmouse)
  98. self.releaser = self.c.mpl_connect("button_press_event", self.releaseonclick)
  99.  
  100. def followmouse(self, event):
  101. if self.o == "h":
  102. self.line.set_ydata([event.ydata, event.ydata])
  103. else:
  104. self.line.set_xdata([event.xdata, event.xdata])
  105. self.c.draw_idle()
  106.  
  107. def releaseonclick(self, event):
  108. if self.o == "h":
  109. self.XorY = self.line.get_ydata()[0]
  110. else:
  111. self.XorY = self.line.get_xdata()[0]
  112.  
  113. print (self.XorY)
  114.  
  115. self.c.mpl_disconnect(self.releaser)
  116. self.c.mpl_disconnect(self.follower)
  117.  
  118. fig = plt.figure()
  119. ax = fig.add_subplot(111)
  120. Vline = draggable_lines(ax, "h", 0.5)
  121. Tline = draggable_lines(ax, "v", 0.5)
  122. Tline2 = draggable_lines(ax, "v", 0.1)
  123. plt.show()
Add Comment
Please, Sign In to add comment