/
mplwidget.py
179 lines (132 loc) · 5.2 KB
/
mplwidget.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
import sys
import os
import time
import numpy as np
# Python Qt4 bindings for GUI objects
from PyQt4 import QtGui
# import the Qt4Agg FigureCanvas object, that binds Figure to
# Qt4Agg backend. It also inherits from QWidget
from matplotlib.backends.backend_qt4agg \
import FigureCanvasQTAgg as FigureCanvas
try:
from matplotlib.backends.backend_qt4agg \
import NavigationToolbar2QTAgg as NavigationToolbar
except ImportError:
from matplotlib.backends.backend_qt4agg \
import NavigationToolbar2QT as NavigationToolbar
# Matplotlib Figure object
from matplotlib.figure import Figure
class MplCanvas(FigureCanvas):
"""Class to represent the FigureCanvas widget"""
def __init__(self, widget):
# setup Matplotlib Figure and Axis
self.fig = Figure()
self.axes = []
# initialization of the canvas
FigureCanvas.__init__(self, self.fig)
# we define the widget as expandable
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
# notify the system of updated policy
FigureCanvas.updateGeometry(self)
# Thanks Laurent
# http://groups.google.com/group/pyinstaller/browse_thread/thread/834bea87c7afcdff?pli=1
class VMToolbar(NavigationToolbar):
def __init__(self, plotCanvas, parent):
NavigationToolbar.__init__(self, plotCanvas, parent)
def _icon(self, name):
# dirty hack to use exclusively .png and thus avoid .svg usage
# because .exe generation is problematic with .svg
name = name.replace('.svg','.png')
return QtGui.QIcon(os.path.join(self.basedir, name))
class MplWidget(QtGui.QWidget):
"""Widget defined in Qt Designer"""
def __init__(self, parent = None):
# initialization of Qt MainWindow widget
QtGui.QWidget.__init__(self, parent)
# set the canvas to the Matplotlib widget
self.canvas = MplCanvas(self)
# Inherit fig and axes from the canvas to avoid indirections
self.axes = self.canvas.axes
self.fig = self.canvas.fig
# When we pack the application with py2exe or py2app .svg graphics
# do not work because the require qt4 plugins that are not properly
# packaged. So we use this woraround, effective at all times in windows
# and macosx.
if not sys.platform in ["win32", "win64", "darwin"]:
self.ntb = NavigationToolbar(self.canvas, self)
else:
self.ntb = VMToolbar(self.canvas, self)
# create a vertical box layout
self.vbl = QtGui.QVBoxLayout()
# add mpl widget and toolbar to vertical box
self.vbl.addWidget(self.canvas)
self.vbl.addWidget(self.ntb)
palette = self.ntb.palette()
self.canvas.fig.set_facecolor([x / 255. for x in
palette.window().color().getRgb()])
# set the layout to th vertical box
self.setLayout(self.vbl)
self.clear_data()
def clear_data(self):
# This is for the correct export of data.
self.xdata = None
self.ydata = []
self.labels = []
def add_axes(self, *args, **kwargs):
""" Adds axes to this widget. """
ax = self.fig.add_axes(*args, **kwargs)
self.axes.append(ax)
return ax
def grid(self):
for ax in self.axes:
ax.grid(ls='-', lw=0.5, c='#cccccc', zorder=-10)
def draw(self):
self.canvas.draw()
def clear(self):
for ax in self.axes:
ax.clear()
self.grid()
self.draw()
self.clear_data()
def set_scales(self, xscale=None, yscale=None, redraw=False):
for ax in self.axes:
if xscale is not None:
ax.set_xscale(xscale)
if yscale is not None:
ax.set_yscale(yscale)
if redraw:
self.draw()
def add_data(self, x, y, label):
if self.xdata is None:
self.xdata = x
self.labels.append('Time')
self.ydata.append(y)
self.labels.append(label)
def savedata(self, fname, location):
d = np.c_[tuple([self.xdata,] + self.ydata)]
with open(fname, "w") as fout:
fout.write("# Input: %s\n" % location)
fout.write("# Date: %s\n" % time.ctime())
fout.write("# Columns:\n")
for c, label in enumerate(self.labels):
fout.write("# %-.3d: %s\n" % (c, label))
np.savetxt(fout, d)
class ConditionsPlotWidget(MplWidget):
def init_axes(self):
self.add_axes([0.1, 0.1, 0.85, 0.85])
self.grid()
class DensityPlotWidget(MplWidget):
def init_axes(self):
self.add_axes([0.085, 0.1, 0.7, 0.85])
self.grid()
class SourcePlotWidget(MplWidget):
def init_axes(self):
self.removalAx = self.add_axes([0.085, 0.1, 0.65, 0.4])
self.creationAx = self.add_axes([0.085, 0.58, 0.65, 0.4])
self.grid()
class RatePlotWidget(MplWidget):
def init_axes(self):
self.add_axes([0.085, 0.1, 0.65, 0.85])
self.grid()