-
Notifications
You must be signed in to change notification settings - Fork 0
/
myNavigation.py
367 lines (335 loc) · 13.1 KB
/
myNavigation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from qtpy.QtCore import Qt, QObject
from qtpy.QtCore import Signal as pyqtSignal
import numpy as np
class myCurve(object):
__points = None
codes = None
l = None
lastindex = 0
__ax = None
def __init__(self, ax):
self.__ax = ax
self.__points = []
def rebuild_line(self):
if not self.__points:
return
xs, ys = list(zip(*self.__points))
if self.l:
self.l.remove()
self.l = plt.Line2D(xs, ys, marker=".", color="black", lw=0.5)
self.__ax.add_line(self.l)
def clear(self):
if not self.l:
return
self.__points = []
self.l.remove()
self.l = None
self.__lastindex = 0
self.__ax.figure.canvas.draw()
def getpoints(self):
return self.__points
def setpoints(self, pnts):
if not pnts:
return
self.__points = pnts
self.rebuild_line()
points = property(getpoints, setpoints)
def add_point(self, point):
if self.__points and point[0] < self.__points[-1][0]:
xs, ys = list(zip(*self.__points))
for i, xx in enumerate(xs):
if xx > point[0]:
break
self.__points.insert(i, point)
self.lastindex = i
else:
self.__points.append(point)
self.lastindex = len(self.__points)-1
self.rebuild_line()
def change_point(self, point):
self.__points[self.lastindex] = point
self.rebuild_line()
class MyNavigation(QObject):
mpl_mv = pyqtSignal(float, float, name='mpl_move')
padWidth = 2
def __init__(self, ax, mplwidget=None):
super(MyNavigation, self).__init__()
self.mplwidget = mplwidget
self.ax = ax
self.figure = ax.figure
self.canvas = ax.figure.canvas
self.zoomer = None
self.mode = "normal"
self.drag = False
self.interval = None
self.distance = None
self.curve = myCurve(self.ax)
self.canvas.mpl_connect("button_press_event", self.on_press)
self.canvas.mpl_connect("button_release_event", self.on_release)
self.canvas.mpl_connect("motion_notify_event", self.on_move)
self.canvas.mpl_connect("scroll_event", self.on_scroll)
self.canvas.mpl_connect("axes_leave_event", self.on_leave_axes)
self.canvas.mpl_connect("axes_enter_event", self.on_enter_axes)
def on_enter_axes(self, event):
if event.inaxes == self.ax and self.mplwidget:
if self.mode == "zoom":
self.mplwidget.setCursor(Qt.CrossCursor)
if self.mode == "pan":
self.mplwidget.setCursor(Qt.OpenHandCursor)
if self.mode == "interval":
self.mplwidget.setCursor(Qt.SizeHorCursor)
if self.mode == "curve":
self.mplwidget.setCursor(Qt.CrossCursor)
def on_leave_axes(self, event):
if event.inaxes == self.ax and self.mplwidget:
self.mplwidget.setCursor(Qt.ArrowCursor)
def on_press(self, event):
if event.inaxes != self.ax:
return
self.startx, self.starty = event.xdata, event.ydata
if event.button == 1 and self.mode == "zoom":
self.bg = self.canvas.copy_from_bbox(self.ax.bbox)
self.zoomer = patches.Rectangle(
(self.startx, self.starty), 0, 0, fill=False, ls="dashed", transform=self.ax.transData)
self.zoomer.set_animated(True)
self.ax.add_patch(self.zoomer)
self.ax.draw_artist(self.zoomer)
self.canvas.blit(self.ax.bbox)
if event.button == 1 and self.mode == "pan":
if self.mplwidget:
self.mplwidget.setCursor(Qt.ClosedHandCursor)
self.pcx_startx, self.pcx_starty = event.x, event.y
self.bg = self.canvas.copy_from_bbox(
self.ax.bbox.padded(-self.padWidth))
self.b = self.bg.get_extents()
for l in self.ax.lines:
l.set_animated(True)
self.ax.patch.set_animated(True)
self.canvas.restore_region(self.bg)
self.canvas.blit(self.ax.bbox.padded(-self.padWidth))
self.drag = True
if event.button == 1 and self.mode == "interval":
self.remove_interval()
self.bg = self.canvas.copy_from_bbox(self.ax.bbox)
self.interval = self.ax.axvspan(
self.startx, self.startx, fc="gray", alpha=0.2)
self.interval.set_animated(True)
self.drag = True
if event.button == 1 and self.mode == "distance":
self.bg = self.canvas.copy_from_bbox(self.ax.bbox)
self.distance, = self.ax.plot([self.startx, self.startx], [
self.starty, self.starty], "black", ls="-.")
self.txt = self.ax.text(
self.startx, self.starty, "(%8.3e,%8.3e)\n%8.3e" % (0, 0, 0), ha='center')
self.txt.set_animated(True)
self.distance.set_animated(True)
self.canvas.restore_region(self.bg)
self.ax.draw_artist(self.txt)
self.canvas.blit(self.ax.bbox)
self.drag = True
if event.button == 1 and self.mode == "curve":
self.curve.add_point((event.xdata, event.ydata))
self.curve.l.set_animated(True)
self.canvas.draw()
self.bg = self.canvas.copy_from_bbox(self.ax.bbox)
self.drag = True
self.canvas.restore_region(self.bg)
self.ax.draw_artist(self.curve.l)
self.canvas.blit(self.ax.bbox)
def on_move(self, event):
if event.inaxes != self.ax:
return
self.curx, self.cury = event.xdata, event.ydata
if self.mplwidget:
self.mpl_mv.emit(self.curx, self.cury)
if self.zoomer is not None:
w = event.xdata-self.startx
h = event.ydata-self.starty
self.zoomer.set_width(w)
self.zoomer.set_height(h)
self.canvas.restore_region(self.bg)
self.ax.draw_artist(self.zoomer)
self.canvas.blit(self.ax.bbox)
if self.mode == "pan" and self.drag:
dx = event.x-self.pcx_startx
dy = event.y-self.pcx_starty
self.bg.set_x(self.b[0]+dx)
self.bg.set_y(self.b[1]-dy)
self.ax.draw_artist(self.ax.patch)
self.canvas.restore_region(self.bg)
self.canvas.blit(self.ax.bbox.padded(-self.padWidth))
if self.mode == "interval" and self.drag:
xy = self.interval.get_xy()
xy[2, 0] = event.xdata
xy[3, 0] = event.xdata
self.interval.set_xy(xy)
self.interval.set_xy(xy)
self.canvas.restore_region(self.bg)
self.ax.draw_artist(self.interval)
self.canvas.blit(self.ax.bbox)
if self.mode == "distance" and self.drag:
self.distance.set_xdata([self.startx, self.curx])
self.distance.set_ydata([self.starty, self.cury])
self.txt.set_x(0.5*(self.startx+self.curx))
self.txt.set_y(0.5*(self.starty+self.cury))
dx = abs(self.startx-self.curx)
dy = abs(self.starty-self.cury)
self.txt.set_text("(%8.3e,%8.3e)\n%8.3e" %
(dx, dy, np.sqrt(dx*dx+dy*dy)))
self.canvas.restore_region(self.bg)
self.ax.draw_artist(self.distance)
self.ax.draw_artist(self.txt)
self.canvas.blit(self.ax.bbox)
if self.mode == "curve" and self.drag:
self.curve.change_point((self.curx, self.cury))
self.canvas.restore_region(self.bg)
self.ax.draw_artist(self.curve.l)
self.canvas.blit(self.ax.bbox)
def on_release(self, event):
if event.inaxes == self.ax:
self.curx, self.cury = event.xdata, event.ydata
if self.zoomer is not None:
x1 = self.startx
x2 = self.startx+self.zoomer.get_width()
y1 = self.starty
y2 = self.starty+self.zoomer.get_height()
if x2 < x1:
x2, x1 = x1, x2
if y2 < y1:
y2, y1 = y1, y2
self.zoomer.remove()
self.zoomer = None
if x1 != x2 and y1 != y2:
self.ax.set_xlim(x1, x2)
self.ax.set_ylim(y1, y2)
self.canvas.draw()
if self.mode == "zoom" and event.button == 3 and event.inaxes == self.ax:
self.ax.autoscale()
self.canvas.draw()
if self.mode == "pan" and self.drag:
if self.mplwidget:
self.mplwidget.setCursor(Qt.OpenHandCursor)
for l in self.ax.lines:
l.set_animated(False)
self.ax.patch.set_animated(False)
self.drag = False
dx = self.curx-self.startx
dy = self.cury-self.starty
b = self.ax.get_xlim()
self.ax.set_xlim(b[0]-dx, b[1]-dx)
b = self.ax.get_ylim()
self.ax.set_ylim(b[0]-dy, b[1]-dy)
self.canvas.draw()
if self.mode == "interval" and self.drag and event.inaxes == self.ax:
self.interval.set_animated(False)
x1, x2 = self.get_interval()
if x1 == x2:
self.remove_interval()
self.canvas.draw()
self.drag = False
if self.mode == "curve" and self.drag and event.inaxes == self.ax:
self.curve.change_point((self.curx, self.cury))
self.curve.l.set_animated(False)
self.canvas.draw()
self.drag = False
if self.mode == "distance" and self.drag and event.inaxes == self.ax:
self.distance.remove()
self.txt.remove()
del self.txt
del self.distance
self.canvas.draw()
self.drag = False
def on_scroll(self, event):
if self.mode != 'zoom':
return
scale = -1
if event.button == "up":
scale = 1
b = self.ax.get_xbound()
self.ax.set_xlim(b[0]-scale*0.1*(b[1]-b[0]),
b[1]+scale*0.1*(b[1]-b[0]))
b = self.ax.get_ybound()
self.ax.set_ylim(b[0]-scale*0.1*(b[1]-b[0]),
b[1]+scale*0.1*(b[1]-b[0]))
self.canvas.draw()
def get_interval(self):
if self.interval:
x1 = self.interval.xy[0, 0]
x2 = self.interval.xy[2, 0]
if x2 < x1:
x1, x2 = x2, x1
return (x1, x2)
else:
return None
def remove_interval(self):
if self.interval:
self.interval.remove()
self.canvas.draw()
del self.interval
self.interval = None
def apply_curve_to_line(self, linenum):
if len(self.ax.get_lines()) < linenum+1:
return
if len(self.curve.points) < 2:
return
c = self.ax.get_lines()[linenum]
xs, ys = list(zip(*np.sort(np.array(self.curve.points,
dtype=[("x", float), ("y", float)]), order="x")))
n1 = np.searchsorted(c.get_xdata(), xs[0])
n2 = np.searchsorted(c.get_xdata(), xs[-1])
xx = c.get_xdata()[n1:n2]
yy = np.interp(xx, xs, ys)
zz = c.get_ydata()
zz[n1:n2] = yy
c.set_ydata(zz)
self.canvas.draw_idle()
def pinch_line_to_curve(self, linenum):
if len(self.ax.get_lines()) < linenum+1:
return
if len(self.curve.points) < 2:
return
c = self.ax.get_lines()[linenum]
xxs, yys = list(zip(*np.sort(np.array(self.curve.points,
dtype=[("x", float), ("y", float)]), order="x")))
n1 = np.searchsorted(c.get_xdata(), xxs[0])
n2 = np.searchsorted(c.get_xdata(), xxs[-1])
xs = c.get_xdata()
ys = c.get_ydata()
def f(x): return np.interp(x, xxs, yys)
for n in range(n1+1, n2):
ss = ys[n]-f(xs[n])
ys[n] = 0.5*ss+f(xs[n])
c.set_data(xs, ys)
self.canvas.draw_idle()
def interpolate_line_on_interval(self, linenum, inteprolation_degree):
if len(self.ax.get_lines()) < linenum+1:
return
c = self.ax.get_lines()[linenum]
xs = c.get_xdata()
ys = c.get_ydata()
if self.get_interval() is not None:
x1, x2 = self.get_interval()
n1 = np.array(xs).searchsorted(x1)
n2 = np.array(xs).searchsorted(x2)
else:
n1 = 0
n2 = len(xs)
if n1 == n2:
return
xs = xs[n1:n2]
ys = ys[n1:n2]
p = np.polyfit(xs, ys, inteprolation_degree)
ys = np.polyval(p, xs)
self.curve.points = list(zip(xs, ys))
self.canvas.draw_idle()
if __name__ == "__main__":
f = plt.figure()
ax = f.add_subplot(111)
nav = MyNavigation(ax)
nav.mode = "zoom"
ax.plot([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [
0, 0.3, 0.2, 0.6, 0.4, -0.5, -0.3])
nav.interpolate_line_on_interval(0, 2)
plt.show()