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