File: looking_glass.py

package info (click to toggle)
matplotlib 1.1.1~rc2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 66,076 kB
  • sloc: python: 90,600; cpp: 69,891; objc: 5,231; ansic: 1,723; makefile: 171; sh: 7
file content (47 lines) | stat: -rw-r--r-- 1,248 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
x, y = np.random.rand(2, 200)

fig = plt.figure()
ax = fig.add_subplot(111)
circ = patches.Circle( (0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circ)


ax.plot(x, y, alpha=0.2)
line, = ax.plot(x, y, alpha=1.0, clip_path=circ)

class EventHandler:
   def __init__(self):
       fig.canvas.mpl_connect('button_press_event', self.onpress)
       fig.canvas.mpl_connect('button_release_event', self.onrelease)
       fig.canvas.mpl_connect('motion_notify_event', self.onmove)
       self.x0, self.y0 = circ.center
       self.pressevent = None

   def onpress(self, event):
      if event.inaxes!=ax:
         return

      if not circ.contains(event)[0]:
         return

      self.pressevent = event

   def onrelease(self, event):
      self.pressevent = None
      self.x0, self.y0 = circ.center

   def onmove(self, event):
      if self.pressevent is None or event.inaxes!=self.pressevent.inaxes:
         return

      dx = event.xdata - self.pressevent.xdata
      dy = event.ydata - self.pressevent.ydata
      circ.center = self.x0 + dx, self.y0 + dy
      line.set_clip_path(circ)
      fig.canvas.draw()

handler = EventHandler()
plt.show()