|
| 1 | +""" |
| 2 | +This is an example to show how to build cross-GUI applications using |
| 3 | +matplotlib event handling to interact with objects on the canvas |
| 4 | +
|
| 5 | +""" |
| 6 | +from matplotlib.patches import Polygon |
| 7 | +from matplotlib.numerix import sqrt, nonzero, equal, asarray, dot, Float |
| 8 | +from matplotlib.numerix.mlab import amin |
| 9 | +from matplotlib.mlab import dist_point_to_segment |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +class EditablePolygon(Polygon): |
| 15 | + """ |
| 16 | + An editable polygon. |
| 17 | +
|
| 18 | + Key-bindings |
| 19 | +
|
| 20 | + 't' toggle vertex markers on and off. When vertex markers are on, |
| 21 | + you can move them, delete them |
| 22 | +
|
| 23 | + 'd' delete the vertex under point |
| 24 | +
|
| 25 | + 'i' insert a vertex at point. You must be within epsilon of the |
| 26 | + line connecting two existing vertices |
| 27 | + |
| 28 | + """ |
| 29 | + |
| 30 | + showverts = True |
| 31 | + epsilon = 5 # max pixel distance to count as a vertex hit |
| 32 | + def __init__(self, *args, **kwargs): |
| 33 | + Polygon.__init__(self, *args, **kwargs) |
| 34 | + self.line = Line2D([],[],marker='o', markerfacecolor='r') |
| 35 | + self._ind = None # the active vert |
| 36 | + |
| 37 | + self.xy = list(self.xy) # make sure it is editable |
| 38 | + |
| 39 | + def set_figure(self, fig): |
| 40 | + Polygon.set_figure(self, fig) |
| 41 | + self.line.set_figure(fig) |
| 42 | + self.figure.canvas.mpl_connect('button_press_event', self.button_press_callback) |
| 43 | + self.figure.canvas.mpl_connect('key_press_event', self.key_press_callback) |
| 44 | + self.figure.canvas.mpl_connect('button_release_event', self.button_release_callback) |
| 45 | + self.figure.canvas.mpl_connect('motion_notify_event', self.motion_notify_callback) |
| 46 | + |
| 47 | + |
| 48 | + def set_transform(self, trans): |
| 49 | + Polygon.set_transform(self, trans) |
| 50 | + self.line.set_transform(trans) |
| 51 | + |
| 52 | + def set_clip_on(self, b): |
| 53 | + Polygon.set_clip_on(self, b) |
| 54 | + self.line.set_clip_on(b) |
| 55 | + |
| 56 | + def set_clip_box(self, b): |
| 57 | + Polygon.set_clip_box(self, b) |
| 58 | + self.line.set_clip_box(b) |
| 59 | + |
| 60 | + |
| 61 | + def draw(self, renderer): |
| 62 | + if not self._visible: return |
| 63 | + Polygon.draw(self, renderer) |
| 64 | + if self.showverts: |
| 65 | + self.line.set_data(zip(*self.xy)) |
| 66 | + self.line.draw(renderer) |
| 67 | + |
| 68 | + def get_ind_under_point(self, event): |
| 69 | + x, y = zip(*self.xy) |
| 70 | + # display coords |
| 71 | + xt, yt = self._transform.numerix_x_y(x, y) |
| 72 | + d = sqrt((xt-event.x)**2 + (yt-event.y)**2) |
| 73 | + indseq = nonzero(equal(d, amin(d))) |
| 74 | + ind = indseq[0] |
| 75 | + if d[ind]>=self.epsilon: |
| 76 | + ind = None |
| 77 | + |
| 78 | + return ind |
| 79 | + |
| 80 | + def button_press_callback(self, event): |
| 81 | + if not self.showverts: return |
| 82 | + if event.inaxes==None: return |
| 83 | + self._ind = self.get_ind_under_point(event) |
| 84 | + |
| 85 | + |
| 86 | + def button_release_callback(self, event): |
| 87 | + if not self.showverts: return |
| 88 | + self._ind = None |
| 89 | + |
| 90 | + def key_press_callback(self, event): |
| 91 | + if not event.inaxes: return |
| 92 | + if event.key=='t': |
| 93 | + self.showverts = not self.showverts |
| 94 | + if not self.showverts: self._ind = None |
| 95 | + elif event.key=='d': |
| 96 | + ind = self.get_ind_under_point(event) |
| 97 | + if ind is not None: |
| 98 | + self.xy = [tup for i,tup in enumerate(self.xy) if i!=ind] |
| 99 | + elif event.key=='i': |
| 100 | + xys = self._transform.seq_xy_tups(self.xy) |
| 101 | + p = event.x, event.y # display coords |
| 102 | + for i in range(len(xys)-1): |
| 103 | + s0 = xys[i] |
| 104 | + s1 = xys[i+1] |
| 105 | + d = dist_point_to_segment(p, s0, s1) |
| 106 | + if d<=self.epsilon: |
| 107 | + self.xy.insert(i+1, (event.xdata, event.ydata)) |
| 108 | + break |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + self.figure.canvas.draw() |
| 114 | + |
| 115 | + def motion_notify_callback(self, event): |
| 116 | + if not self.showverts: return |
| 117 | + if self._ind is None: return |
| 118 | + if event.inaxes is None: return |
| 119 | + x,y = event.xdata, event.ydata |
| 120 | + self.xy[self._ind] = x,y |
| 121 | + self.figure.canvas.draw_idle() |
| 122 | + |
| 123 | + |
| 124 | +from pylab import * |
| 125 | +verts = Circle((.5,.5),.5).get_verts() |
| 126 | +p = EditablePolygon(verts) |
| 127 | + |
| 128 | +fig = figure() |
| 129 | +ax = subplot(111) |
| 130 | +ax.add_patch(p) |
| 131 | +title('Click and drag a point to move it') |
| 132 | +axis([0,1,0,1]) |
| 133 | +show() |
0 commit comments