44
55"""
66from matplotlib .artist import Artist
7- from matplotlib .patches import Polygon
7+ from matplotlib .patches import Polygon , CirclePolygon
88from matplotlib .numerix import sqrt , nonzero , equal , asarray , dot , Float
99from matplotlib .numerix .mlab import amin
1010from matplotlib .mlab import dist_point_to_segment
@@ -31,26 +31,34 @@ class PolygonInteractor:
3131 showverts = True
3232 epsilon = 5 # max pixel distance to count as a vertex hit
3333
34- def __init__ (self , poly ):
34+ def __init__ (self , ax , poly ):
3535 if poly .figure is None :
3636 raise RuntimeError ('You must first add the polygon to a figure or canvas before defining the interactor' )
37+ self .ax = ax
3738 canvas = poly .figure .canvas
3839 self .poly = poly
3940 self .poly .verts = list (self .poly .verts )
4041 x , y = zip (* self .poly .verts )
41- self .line = Line2D (x ,y ,marker = 'o' , markerfacecolor = 'r' )
42+ self .line = Line2D (x ,y ,marker = 'o' , markerfacecolor = 'r' , animated = True )
4243 #self._update_line(poly)
4344
4445 cid = self .poly .add_callback (self .poly_changed )
4546 self ._ind = None # the active vert
4647
48+ canvas .mpl_connect ('draw_event' , self .draw_callback )
4749 canvas .mpl_connect ('button_press_event' , self .button_press_callback )
4850 canvas .mpl_connect ('key_press_event' , self .key_press_callback )
4951 canvas .mpl_connect ('button_release_event' , self .button_release_callback )
5052 canvas .mpl_connect ('motion_notify_event' , self .motion_notify_callback )
5153 self .canvas = canvas
5254
5355
56+ def draw_callback (self , event ):
57+ self .background = self .canvas .copy_from_bbox (self .ax .bbox )
58+ self .ax .draw_artist (self .poly )
59+ self .ax .draw_artist (self .line )
60+ self .canvas .blit (self .ax .bbox )
61+
5462 def poly_changed (self , poly ):
5563 'this method is called whenever the polygon object is called'
5664 # only copy the artist props to the line (except visibility)
@@ -123,7 +131,12 @@ def motion_notify_callback(self, event):
123131 x ,y = event .xdata , event .ydata
124132 self .poly .verts [self ._ind ] = x ,y
125133 self .line .set_data (zip (* self .poly .verts ))
126- self .canvas .draw_idle ()
134+
135+ self .canvas .restore_region (self .background )
136+ self .ax .draw_artist (self .poly )
137+ self .ax .draw_artist (self .line )
138+ self .canvas .blit (self .ax .bbox )
139+
127140
128141
129142from pylab import *
@@ -133,14 +146,14 @@ def motion_notify_callback(self, event):
133146
134147
135148fig = figure ()
136- circ = Circle ((.5 ,.5 ),.5 )
149+ circ = CirclePolygon ((.5 ,.5 ),.5 , animated = True )
137150
138151
139152
140153
141154ax = subplot (111 )
142155ax .add_patch (circ )
143- p = PolygonInteractor ( circ )
156+ p = PolygonInteractor ( ax , circ )
144157
145158ax .add_line (p .line )
146159ax .set_title ('Click and drag a point to move it' )
0 commit comments