[82a54b8] | 1 | """ |
---|
| 2 | This module allows more interaction with the plot |
---|
| 3 | """ |
---|
| 4 | import plottables |
---|
| 5 | from BaseInteractor import _BaseInteractor |
---|
| 6 | |
---|
| 7 | class PointInteractor(_BaseInteractor): |
---|
| 8 | """ |
---|
| 9 | """ |
---|
| 10 | def __init__(self, base, axes, color='black', zorder=3, id=''): |
---|
| 11 | """ |
---|
| 12 | """ |
---|
| 13 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 14 | self.zorder = zorder |
---|
| 15 | self.id = id |
---|
| 16 | self.color = color |
---|
| 17 | self.colorlist = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] |
---|
[10bfeb3] | 18 | self.symbollist = ['o', 'x', '^', 'v', '<', '>', |
---|
[6c4130a] | 19 | '+', 's', 'd', 'D', 'h', 'H', 'p', '-', '--', |
---|
| 20 | 'vline', 'step'] |
---|
[82a54b8] | 21 | self.markersize = None |
---|
| 22 | self.marker = None |
---|
| 23 | self.marker2 = None |
---|
| 24 | self._button_down = False |
---|
| 25 | self._context_menu = False |
---|
| 26 | self._dragged = False |
---|
| 27 | self.connect_markers([self.axes]) |
---|
| 28 | |
---|
| 29 | def _color(self, c): |
---|
| 30 | """Return a particular colour""" |
---|
| 31 | return self.colorlist[c % len(self.colorlist)] |
---|
| 32 | |
---|
| 33 | def _symbol(self, s): |
---|
| 34 | """Return a particular symbol""" |
---|
| 35 | return self.symbollist[s % len(self.symbollist)] |
---|
| 36 | |
---|
[2acedc8] | 37 | def points(self, x, y, dx=None, dy=None, color=0, symbol=0, zorder=1, |
---|
| 38 | markersize=5, label=None, hide_error=False): |
---|
[82a54b8] | 39 | """ |
---|
| 40 | """ |
---|
| 41 | #Draw curve |
---|
[6c4130a] | 42 | if self._symbol(symbol) == '-' or self._symbol(symbol) == '--': |
---|
[69e0760] | 43 | l_width = markersize * 0.4 |
---|
[6c4130a] | 44 | return self.curve(x=x, y=y, color=color, symbol=symbol, |
---|
| 45 | label=label, width=l_width) |
---|
[82a54b8] | 46 | #return |
---|
[6c4130a] | 47 | if self._symbol(symbol) == 'vline': |
---|
| 48 | l_width = markersize * 0.4 |
---|
| 49 | return self.vline(x=x, y=y, color=color, |
---|
| 50 | label=label, width=l_width) |
---|
| 51 | if self._symbol(symbol) == 'step': |
---|
| 52 | l_width = markersize * 0.4 |
---|
| 53 | return self.step(x=x, y=y, color=color, |
---|
| 54 | label=label, width=l_width) |
---|
[82a54b8] | 55 | if not self.marker == None: |
---|
| 56 | self.base.connect.clear([self.marker]) |
---|
| 57 | self.color = self._color(color) |
---|
| 58 | if self.markersize != None: |
---|
| 59 | markersize = self.markersize |
---|
| 60 | # Convert tuple (lo,hi) to array [(x-lo),(hi-x)] |
---|
| 61 | if dx != None and type(dx) == type(()): |
---|
| 62 | dx = nx.vstack((x-dx[0], dx[1]-x)).transpose() |
---|
| 63 | if dy != None and type(dy) == type(()): |
---|
| 64 | dy = nx.vstack((y-dy[0], dy[1]-y)).transpose() |
---|
[2acedc8] | 65 | |
---|
[82a54b8] | 66 | if dx == None and dy == None: |
---|
[6c4130a] | 67 | #zorder = 1 |
---|
[82a54b8] | 68 | self.marker = self.axes.plot(x, y, color=self.color, |
---|
| 69 | marker=self._symbol(symbol), |
---|
| 70 | markersize=markersize, |
---|
| 71 | linestyle='', label=label, |
---|
| 72 | zorder=zorder)[0] |
---|
| 73 | else: |
---|
| 74 | |
---|
| 75 | if hide_error: |
---|
[6c4130a] | 76 | #zorder = 1 |
---|
[82a54b8] | 77 | self.marker = self.axes.plot(x, y, color=self.color, |
---|
[10bfeb3] | 78 | marker=self._symbol(symbol), |
---|
| 79 | markersize=markersize, |
---|
| 80 | linestyle='', label=label, |
---|
[2acedc8] | 81 | zorder=1)[0] |
---|
[82a54b8] | 82 | else: |
---|
[6c4130a] | 83 | #zorder = 2 |
---|
[82a54b8] | 84 | self.marker = self.axes.errorbar(x, y, yerr=dy, |
---|
[10bfeb3] | 85 | xerr=None, |
---|
| 86 | ecolor=self.color, |
---|
| 87 | color=self.color, |
---|
| 88 | capsize=2, |
---|
| 89 | linestyle='', |
---|
| 90 | barsabove=False, |
---|
| 91 | marker=self._symbol(symbol), |
---|
| 92 | markersize=markersize, |
---|
| 93 | lolims=False, uplims=False, |
---|
| 94 | xlolims=False, xuplims=False, |
---|
| 95 | label=label, |
---|
[2acedc8] | 96 | zorder=1)[0] |
---|
[82a54b8] | 97 | |
---|
| 98 | self.connect_markers([self.marker]) |
---|
| 99 | self.update() |
---|
| 100 | |
---|
[2acedc8] | 101 | def curve(self, x, y, dy=None, color=0, symbol=0, zorder=10, |
---|
| 102 | label=None, width=2.0): |
---|
[82a54b8] | 103 | """ |
---|
| 104 | """ |
---|
| 105 | if not self.marker == None: |
---|
| 106 | self.base.connect.clear([self.marker]) |
---|
| 107 | self.color = self._color(color) |
---|
[69e0760] | 108 | self.marker = self.axes.plot(x, y, color=self.color, lw=width, |
---|
[6c4130a] | 109 | marker='', linestyle=self._symbol(symbol), |
---|
[2acedc8] | 110 | label=label, zorder=zorder)[0] |
---|
[82a54b8] | 111 | |
---|
| 112 | self.connect_markers([self.marker]) |
---|
| 113 | self.update() |
---|
[6c4130a] | 114 | |
---|
[82a54b8] | 115 | |
---|
[2acedc8] | 116 | def vline(self, x, y, dy=None, color=0, symbol=0, zorder=1, |
---|
| 117 | label=None, width=2.0): |
---|
[6c4130a] | 118 | """ |
---|
| 119 | """ |
---|
| 120 | if not self.marker == None: |
---|
| 121 | self.base.connect.clear([self.marker]) |
---|
| 122 | self.color = self._color(color) |
---|
| 123 | if min(y) < 0: |
---|
| 124 | y_min = 0.0 |
---|
| 125 | else: |
---|
| 126 | y_min = min(y)*9/10 |
---|
| 127 | self.marker = self.axes.vlines(x=x, ymin=y_min, ymax=y, |
---|
| 128 | color=self.color, |
---|
| 129 | linestyle='-', label=label, |
---|
[2acedc8] | 130 | lw=width, zorder=zorder) |
---|
[6c4130a] | 131 | self.connect_markers([self.marker]) |
---|
| 132 | self.update() |
---|
| 133 | |
---|
[2acedc8] | 134 | def step(self, x, y, dy=None, color=0, symbol=0, zorder=1, |
---|
| 135 | label=None, width=2.0): |
---|
[6c4130a] | 136 | """ |
---|
| 137 | """ |
---|
| 138 | if not self.marker == None: |
---|
| 139 | self.base.connect.clear([self.marker]) |
---|
| 140 | self.color = self._color(color) |
---|
| 141 | if self.markersize != None: |
---|
| 142 | markersize = self.markersize |
---|
| 143 | |
---|
| 144 | self.marker = self.axes.step(x, y, color=self.color, |
---|
| 145 | marker='', |
---|
| 146 | linestyle='-', label=label, |
---|
[2acedc8] | 147 | lw=width, zorder=zorder)[0] |
---|
[6c4130a] | 148 | self.connect_markers([self.marker]) |
---|
| 149 | self.update() |
---|
| 150 | |
---|
[82a54b8] | 151 | def connect_markers(self, markers): |
---|
| 152 | """ |
---|
| 153 | Connect markers to callbacks |
---|
| 154 | """ |
---|
| 155 | for h in markers: |
---|
| 156 | connect = self.base.connect |
---|
| 157 | connect('enter', h, self._on_enter) |
---|
| 158 | connect('leave', h, self._on_leave) |
---|
| 159 | connect('click', h, self._on_click) |
---|
| 160 | connect('release', h, self._on_release) |
---|
| 161 | connect('key', h, self.onKey) |
---|
| 162 | |
---|
| 163 | def clear(self): |
---|
| 164 | print "plottable_interactor.clear()" |
---|
| 165 | |
---|
| 166 | def _on_click(self, evt): |
---|
| 167 | """ |
---|
| 168 | Called when a mouse button is clicked |
---|
| 169 | from within the boundaries of an artist. |
---|
| 170 | """ |
---|
| 171 | if self._context_menu == True: |
---|
| 172 | self._context_menu = False |
---|
| 173 | evt.artist = self.marker |
---|
| 174 | self._on_leave(evt) |
---|
| 175 | |
---|
[10bfeb3] | 176 | def _on_release(self, evt): |
---|
[82a54b8] | 177 | """ |
---|
| 178 | Called when a mouse button is released |
---|
| 179 | within the boundaries of an artist |
---|
| 180 | """ |
---|
[10bfeb3] | 181 | # Check to see whether we are about to pop |
---|
[82a54b8] | 182 | # the context menu up |
---|
| 183 | if evt.button == 3: |
---|
| 184 | self._context_menu = True |
---|
| 185 | |
---|
| 186 | def _on_enter(self, evt): |
---|
| 187 | """ |
---|
| 188 | Called when we are entering the boundaries |
---|
| 189 | of an artist. |
---|
| 190 | """ |
---|
| 191 | if not evt.artist.__class__.__name__ == "AxesSubplot": |
---|
| 192 | self.base.plottable_selected(self.id) |
---|
| 193 | |
---|
| 194 | if evt.artist.get_color() == 'y': |
---|
| 195 | try: |
---|
[10bfeb3] | 196 | evt.artist.set_color('b') |
---|
[82a54b8] | 197 | except: |
---|
| 198 | evt.artist.set_color_cycle('b') |
---|
| 199 | if hasattr(evt.artist, "set_facecolor"): |
---|
| 200 | evt.artist.set_facecolor('b') |
---|
| 201 | if hasattr(evt.artist, "set_edgecolor"): |
---|
| 202 | evt.artist.set_edgecolor('b') |
---|
| 203 | else: |
---|
| 204 | try: |
---|
[10bfeb3] | 205 | evt.artist.set_color('y') |
---|
[82a54b8] | 206 | except: |
---|
| 207 | evt.artist.set_color_cycle('y') |
---|
| 208 | if hasattr(evt.artist, "set_facecolor"): |
---|
| 209 | evt.artist.set_facecolor('y') |
---|
| 210 | if hasattr(evt.artist, "set_edgecolor"): |
---|
| 211 | evt.artist.set_edgecolor('y') |
---|
| 212 | |
---|
| 213 | self.axes.figure.canvas.draw_idle() |
---|
| 214 | |
---|
| 215 | def _on_leave(self, evt): |
---|
| 216 | """ |
---|
| 217 | Called when we are leaving the boundaries |
---|
| 218 | of an artist. |
---|
| 219 | """ |
---|
| 220 | if not evt.artist.__class__.__name__ == "AxesSubplot": |
---|
| 221 | if self._context_menu == False: |
---|
| 222 | self.base.plottable_selected(None) |
---|
| 223 | try: |
---|
| 224 | evt.artist.set_color(self.color) |
---|
| 225 | except: |
---|
| 226 | evt.artist.set_color_cycle(self.color) |
---|
| 227 | if hasattr(evt.artist, "set_facecolor"): |
---|
| 228 | evt.artist.set_facecolor(self.color) |
---|
[10bfeb3] | 229 | if hasattr(evt.artist, "set_edgecolor"): |
---|
[82a54b8] | 230 | evt.artist.set_edgecolor(self.color) |
---|
| 231 | self.axes.figure.canvas.draw_idle() |
---|
| 232 | |
---|
| 233 | def update(self): |
---|
| 234 | """ |
---|
| 235 | Update |
---|
| 236 | """ |
---|
| 237 | pass |
---|