Menu

[r3984]: / branches / transforms / examples / clippath_test.py  Maximize  Restore  History

Download this file

56 lines (44 with data), 1.6 kB

 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
48
49
50
51
52
53
54
55
from pylab import figure, show, nx
import matplotlib.transforms as transforms
from matplotlib.patches import RegularPolygon
import matplotlib.agg as agg
class ClipWindow:
def __init__(self, ax, line):
self.ax = ax
ax.set_title('drag polygon around to test clipping')
self.canvas = ax.figure.canvas
self.line = line
self.poly = RegularPolygon(
(200, 200), numVertices=10, radius=100,
facecolor='yellow', alpha=0.25,
transform=transforms.IdentityTransform())
ax.add_patch(self.poly)
self.canvas.mpl_connect('button_press_event', self.onpress)
self.canvas.mpl_connect('button_release_event', self.onrelease)
self.canvas.mpl_connect('motion_notify_event', self.onmove)
self.x, self.y = None, None
def onpress(self, event):
self.x, self.y = event.x, event.y
def onrelease(self, event):
self.x, self.y = None, None
def onmove(self, event):
if self.x is None: return
dx = event.x - self.x
dy = event.y - self.y
self.x, self.y = event.x, event.y
x, y = self.poly.xy
x += dx
y += dy
#print self.y, event.y, dy, y
self.poly.xy = x,y
self._clip()
def _clip(self):
self.line.set_clip_path(self.poly.get_path(), self.poly.get_transform())
self.canvas.draw_idle()
fig = figure(figsize=(8,8))
ax = fig.add_subplot(111)
t = nx.arange(0.0, 4.0, 0.01)
s = 2*nx.sin(2*nx.pi*8*t)
line, = ax.plot(t, 2*(nx.mlab.rand(len(t))-0.5), 'b-')
clipwin = ClipWindow(ax, line)
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.