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'] |
---|
18 | self.symbollist = ['o', 'x', '^', 'v', '<', '>', |
---|
19 | '+', 's', 'd', 'D', 'h', 'H', 'p', '-', '--', |
---|
20 | 'vline', 'step'] |
---|
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 | |
---|
37 | def points(self, x, y, dx=None, dy=None, color=0, symbol=0, zorder=1, |
---|
38 | markersize=5, label=None, hide_error=False): |
---|
39 | """ |
---|
40 | """ |
---|
41 | #Draw curve |
---|
42 | if self._symbol(symbol) == '-' or self._symbol(symbol) == '--': |
---|
43 | l_width = markersize * 0.4 |
---|
44 | return self.curve(x=x, y=y, color=color, symbol=symbol, |
---|
45 | label=label, width=l_width) |
---|
46 | #return |
---|
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) |
---|
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() |
---|
65 | |
---|
66 | if dx == None and dy == None: |
---|
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 | """ |
---|
105 | if not self.marker == None: |
---|
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 | """ |
---|
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, |
---|
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 | """ |
---|
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, |
---|
147 | lw=width, zorder=zorder)[0] |
---|
148 | self.connect_markers([self.marker]) |
---|
149 | self.update() |
---|
150 | |
---|
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 | |
---|
176 | def _on_release(self, evt): |
---|
177 | """ |
---|
178 | Called when a mouse button is released |
---|
179 | within the boundaries of an artist |
---|
180 | """ |
---|
181 | # Check to see whether we are about to pop |
---|
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: |
---|
196 | evt.artist.set_color('b') |
---|
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: |
---|
205 | evt.artist.set_color('y') |
---|
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) |
---|
229 | if hasattr(evt.artist, "set_edgecolor"): |
---|
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 |
---|