-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutplot_pyqt_tab.py
132 lines (112 loc) · 5.02 KB
/
outplot_pyqt_tab.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
from __future__ import unicode_literals
import matplotlib.pyplot as plt
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
################################################################
# Mouse events to zoom,pan... on plot
################################################################
class ZoomPan:
def __init__(self):
self.press = None
self.cur_xlim = None
self.cur_ylim = None
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.xpress = None
self.ypress = None
def zoom_function(self, ax, base_scale = 2.):
def zoom(event):
cur_xlim = ax.get_xlim()
cur_ylim = ax.get_ylim()
xdata = event.xdata # get event x location
ydata = event.ydata # get event y location
if event.button == 'down':
# deal with zoom in
scale_factor = 1 / base_scale
elif event.button == 'up':
# deal with zoom out
scale_factor = base_scale
else:
# deal with something that should never happen
scale_factor = 1
print(event.button)
new_width = (cur_xlim[1] - cur_xlim[0]) * scale_factor
new_height = (cur_ylim[1] - cur_ylim[0]) * scale_factor
relx = (cur_xlim[1] - xdata)/(cur_xlim[1] - cur_xlim[0])
rely = (cur_ylim[1] - ydata)/(cur_ylim[1] - cur_ylim[0])
ax.set_xlim([xdata - new_width * (1-relx), xdata + new_width * (relx)])
ax.set_ylim([ydata - new_height * (1-rely), ydata + new_height * (rely)])
ax.figure.canvas.draw()
fig = ax.get_figure() # get the figure of interest
fig.canvas.mpl_connect('scroll_event', zoom)
return zoom
def pan_function(self, ax):
def onPress(event):
if event.inaxes != ax: return
self.cur_xlim = ax.get_xlim()
self.cur_ylim = ax.get_ylim()
self.press = self.x0, self.y0, event.xdata, event.ydata
self.x0, self.y0, self.xpress, self.ypress = self.press
def onRelease(event):
self.press = None
ax.figure.canvas.draw()
def onMotion(event):
if self.press is None: return
if event.inaxes != ax: return
dx = event.xdata - self.xpress
dy = event.ydata - self.ypress
self.cur_xlim -= dx
self.cur_ylim -= dy
ax.set_xlim(self.cur_xlim)
ax.set_ylim(self.cur_ylim)
ax.figure.canvas.draw()
fig = ax.get_figure() # get the figure of interest
# attach the call back
fig.canvas.mpl_connect('button_press_event',onPress)
fig.canvas.mpl_connect('button_release_event',onRelease)
fig.canvas.mpl_connect('motion_notify_event',onMotion)
#return the function
return onMotion
def Zoom_Extent(self, ax,xl,yl):
def ondblClick(event):
if event.dblclick and event.inaxes == ax:
# Plotting back to full extent or zoom extent of figure
ax.set_xlim(0,xl)
ax.set_ylim(yl,0)
ax.figure.canvas.draw()
fig = ax.get_figure()
fig.canvas.mpl_connect('button_press_event',ondblClick)
################################################################
# Canvas to plot figure
################################################################
class MplCanvas(FigureCanvas):
def __init__(self,parent,data):
# self.dt = dt
self.data = data
self.fig = plt.figure(figsize=(20, 10), facecolor='w', edgecolor='w')
self.axis = self.fig.add_subplot(111)
self.axis.set_axis_off()
self.im = self.axis.imshow(self.data, cmap='gray', vmin=0, vmax=255)
# Adjusting padding/margins of the plot in all directions
self.fig.subplots_adjust(right=0.95,
left=0.07,
top=0.93,
bottom=0.08)
plt.close(self.fig)
FigureCanvas.__init__(self, self.fig)
self.setParent(None)
FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
# For Zoom and pan functions with mouse buttons
zp = ZoomPan()
zp.zoom_function(self.axis, base_scale = 1.1)
zp.pan_function(self.axis)
xl,yl= data.shape[1], data.shape[0] # Default extent of figure.
zp.Zoom_Extent(self.axis,xl,yl)
# Navigation toolbar
self.Nav_toolbar = NavigationToolbar(self,self)