Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91c1491

Browse files
committed
added poly editor
svn path=/trunk/matplotlib/; revision=3602
1 parent 941792c commit 91c1491

3 files changed

Lines changed: 242 additions & 84 deletions

File tree

examples/poly_editor.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def __init__(self, ax, poly):
3737
self.ax = ax
3838
canvas = poly.figure.canvas
3939
self.poly = poly
40-
self.poly.verts = list(self.poly.verts)
41-
x, y = zip(*self.poly.verts)
40+
41+
x, y = zip(*self.poly.xy)
4242
self.line = Line2D(x,y,marker='o', markerfacecolor='r', animated=True)
4343
#self._update_line(poly)
4444

@@ -69,7 +69,7 @@ def poly_changed(self, poly):
6969

7070
def get_ind_under_point(self, event):
7171
'get the index of the vertex under point if within epsilon tolerance'
72-
x, y = zip(*self.poly.verts)
72+
x, y = zip(*self.poly.xy)
7373

7474
# display coords
7575
xt, yt = self.poly.get_transform().numerix_x_y(x, y)
@@ -105,18 +105,18 @@ def key_press_callback(self, event):
105105
elif event.key=='d':
106106
ind = self.get_ind_under_point(event)
107107
if ind is not None:
108-
self.poly.verts = [tup for i,tup in enumerate(self.poly.verts) if i!=ind]
109-
self.line.set_data(zip(*self.poly.verts))
108+
self.poly.xy = [tup for i,tup in enumerate(self.poly.xy) if i!=ind]
109+
self.line.set_data(zip(*self.poly.xy))
110110
elif event.key=='i':
111-
xys = self.poly.get_transform().seq_xy_tups(self.poly.verts)
111+
xys = self.poly.get_transform().seq_xy_tups(self.poly.xy)
112112
p = event.x, event.y # display coords
113113
for i in range(len(xys)-1):
114114
s0 = xys[i]
115115
s1 = xys[i+1]
116116
d = dist_point_to_segment(p, s0, s1)
117117
if d<=self.epsilon:
118-
self.poly.verts.insert(i+1, (event.xdata, event.ydata))
119-
self.line.set_data(zip(*self.poly.verts))
118+
self.poly.xy.insert(i+1, (event.xdata, event.ydata))
119+
self.line.set_data(zip(*self.poly.xy))
120120
break
121121

122122

@@ -129,8 +129,8 @@ def motion_notify_callback(self, event):
129129
if event.inaxes is None: return
130130
if event.button != 1: return
131131
x,y = event.xdata, event.ydata
132-
self.poly.verts[self._ind] = x,y
133-
self.line.set_data(zip(*self.poly.verts))
132+
self.poly.xy[self._ind] = x,y
133+
self.line.set_data(zip(*self.poly.xy))
134134

135135
self.canvas.restore_region(self.background)
136136
self.ax.draw_artist(self.poly)
@@ -146,17 +146,23 @@ def motion_notify_callback(self, event):
146146

147147

148148
fig = figure()
149-
circ = CirclePolygon((.5,.5),.5, animated=True)
149+
theta = arange(0, 2*pi, 0.1)
150+
r = 1.5
151+
152+
xs = r*npy.cos(theta)
153+
ys = r*npy.sin(theta)
154+
155+
poly = Polygon(zip(xs, ys,), animated=True)
150156

151157

152158

153159

154160
ax = subplot(111)
155-
ax.add_patch(circ)
156-
p = PolygonInteractor( ax, circ)
161+
ax.add_patch(poly)
162+
p = PolygonInteractor( ax, poly)
157163

158164
ax.add_line(p.line)
159165
ax.set_title('Click and drag a point to move it')
160-
ax.set_xlim((0,1))
161-
ax.set_ylim((0,1))
166+
ax.set_xlim((-2,2))
167+
ax.set_ylim((-2,2))
162168
show()

lib/matplotlib/patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def __init__(self, xy, numVertices, radius=5, orientation=0,
438438
"""
439439
Patch.__init__(self, **kwargs)
440440

441-
self.xy = xy
441+
self.xy = list(xy)
442442
self.numVertices = numVertices
443443
self.radius = radius
444444
self.orientation = orientation

0 commit comments

Comments
 (0)