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