1 | import plottables |
---|
2 | from BaseInteractor import _BaseInteractor |
---|
3 | |
---|
4 | class PointInteractor(_BaseInteractor): |
---|
5 | |
---|
6 | def __init__(self,base,axes,color='black', zorder=3, id=''): |
---|
7 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
8 | self.zorder = zorder |
---|
9 | self.id = id |
---|
10 | self.colorlist = ['b','g','r','c','m','y'] |
---|
11 | self.symbollist = ['o','x','^','v','<','>','+','s','d','D','h','H','p'] |
---|
12 | self.marker = None |
---|
13 | self.marker2 = None |
---|
14 | self._button_down = False |
---|
15 | self._context_menu = False |
---|
16 | self._dragged = False |
---|
17 | |
---|
18 | def _color(self,c): |
---|
19 | """Return a particular colour""" |
---|
20 | return self.colorlist[c%len(self.colorlist)] |
---|
21 | |
---|
22 | def _symbol(self,s): |
---|
23 | """Return a particular symbol""" |
---|
24 | return self.symbollist[s%len(self.symbollist)] |
---|
25 | |
---|
26 | |
---|
27 | def points(self,x,y,dx=None,dy=None,color=0,symbol=0,label=None): |
---|
28 | |
---|
29 | if not self.marker==None: |
---|
30 | self.base.connect.clear([self.marker]) |
---|
31 | |
---|
32 | self.color = self._color(color) |
---|
33 | |
---|
34 | # Convert tuple (lo,hi) to array [(x-lo),(hi-x)] |
---|
35 | if dx != None and type(dx) == type(()): |
---|
36 | dx = nx.vstack((x-dx[0],dx[1]-x)).transpose() |
---|
37 | if dy != None and type(dy) == type(()): |
---|
38 | dy = nx.vstack((y-dy[0],dy[1]-y)).transpose() |
---|
39 | |
---|
40 | if dx==None and dy==None: |
---|
41 | self.marker = self.axes.plot(x,y,color=self.color, |
---|
42 | marker=self._symbol(symbol),linestyle='',label=label, |
---|
43 | zorder=self.zorder)[0] |
---|
44 | else: |
---|
45 | self.marker = self.axes.errorbar(x, y, yerr=dy, xerr=None, |
---|
46 | ecolor=self.color, |
---|
47 | color=self.color, |
---|
48 | capsize=2,linestyle='', barsabove=False, |
---|
49 | #mec=self.color, mfc=self.color, |
---|
50 | marker=self._symbol(symbol), |
---|
51 | lolims=False, uplims=False, |
---|
52 | xlolims=False, xuplims=False,label=label, |
---|
53 | zorder=self.zorder)[0] |
---|
54 | |
---|
55 | self.connect_markers([self.marker]) |
---|
56 | self.update() |
---|
57 | |
---|
58 | def curve(self, x, y, dy=None, color=0, symbol=0, label=None): |
---|
59 | |
---|
60 | if not self.marker==None: |
---|
61 | self.base.connect.clear([self.marker]) |
---|
62 | |
---|
63 | self.color = self._color(color) |
---|
64 | |
---|
65 | self.marker = self.axes.plot(x,y,color=self.color,marker='',linestyle='-',label=label)[0] |
---|
66 | |
---|
67 | self.connect_markers([self.marker]) |
---|
68 | self.update() |
---|
69 | |
---|
70 | def connect_markers(self,markers): |
---|
71 | """ |
---|
72 | Connect markers to callbacks |
---|
73 | """ |
---|
74 | for h in markers: |
---|
75 | connect = self.base.connect |
---|
76 | connect('enter', h, self._on_enter) |
---|
77 | connect('leave', h, self._on_leave) |
---|
78 | connect('click', h, self._on_click) |
---|
79 | connect('release', h, self._on_release) |
---|
80 | #connect('drag', h, self._on_drag) |
---|
81 | connect('key', h, self.onKey) |
---|
82 | |
---|
83 | def clear(self): |
---|
84 | print "plottable_interactor.clear()" |
---|
85 | |
---|
86 | def _on_click(self, evt): |
---|
87 | self._context_menu = False |
---|
88 | |
---|
89 | def _on_release(self, evt): |
---|
90 | # Check to see whether we are about to pop |
---|
91 | # the context menu up |
---|
92 | if evt.button==3: |
---|
93 | self._context_menu = True |
---|
94 | |
---|
95 | def _on_enter(self, evt): |
---|
96 | self.base.plottable_selected(self.id) |
---|
97 | evt.artist.set_color('y') |
---|
98 | if hasattr(evt.artist, "set_facecolor"): |
---|
99 | evt.artist.set_facecolor('y') |
---|
100 | if hasattr(evt.artist, "set_edgecolor"): |
---|
101 | evt.artist.set_edgecolor('y') |
---|
102 | self.axes.figure.canvas.draw_idle() |
---|
103 | |
---|
104 | def _on_leave(self, evt): |
---|
105 | if self._context_menu==False: |
---|
106 | self.base.plottable_selected(None) |
---|
107 | evt.artist.set_color(self.color) |
---|
108 | if hasattr(evt.artist, "set_facecolor"): |
---|
109 | evt.artist.set_facecolor(self.color) |
---|
110 | if hasattr(evt.artist, "set_edgecolor"): |
---|
111 | evt.artist.set_edgecolor(self.color) |
---|
112 | self.axes.figure.canvas.draw_idle() |
---|
113 | |
---|
114 | self._context_menu = False |
---|
115 | |
---|
116 | def update(self): |
---|
117 | """ |
---|
118 | Update |
---|
119 | """ |
---|
120 | print "update" |
---|
121 | |
---|
122 | |
---|