-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_series.py
More file actions
330 lines (280 loc) · 12.8 KB
/
plot_series.py
File metadata and controls
330 lines (280 loc) · 12.8 KB
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
import numpy as np
from getNeigh import getNeighbor
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import os
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.image as mpimg
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
class TimeSeries(QWidget):
def __init__(self, label, parent = None):
super(TimeSeries, self).__init__(parent)
self.label = label
self.init()
self.implementGrid()
self.new_series = []
self.new_series_labels = []
def init(self):
# FigureCanvas to show the mesh in
self.figure = plt.figure()
self.figure.set_facecolor((45/255, 45/255, 45/255))
self.canvas = FigureCanvas(self.figure)
self.canvas.setFixedSize(750,276)
self.figure.subplots_adjust(left = 0.1, right = 0.94, bottom = 0.1, top = 0.9)
self.ax = self.figure.add_subplot(111)
self.ax.set_xlim(0,1.25)
self.ax.set_facecolor((45/255, 45/255, 45/255))
broken_white = (150/255, 150/255, 150/255)
self.ax.grid('on', color = broken_white)
self.ax.spines['bottom'].set_color(broken_white)
self.ax.spines['top'].set_color(broken_white)
self.ax.spines['right'].set_color(broken_white)
self.ax.spines['left'].set_color(broken_white)
self.ax.tick_params(axis='x', colors=broken_white, labelsize = 7)
self.ax.tick_params(axis='y', colors=broken_white, labelsize = 7)
self.ax.xaxis.label.set_color(broken_white)
self.ax.yaxis.label.set_color(broken_white)
self.ax.set_ylabel(self.label, fontweight = 'bold', fontsize = 9)
self.Export = QPushButton()
self.Export.setToolTip('Export graph as PNG')
im = QIcon('support_files/export.png')
self.Export.setIcon(im)
self.Export.setDisabled(True)
self.Export.clicked.connect(self.exportGraph)
self.Export.setStyleSheet("""
QPushButton {
border-width: 25px solid white;
border-radius: 0px;
color: rgb(180,180,180);
background-color: rgb(55, 55, 60, 0);
}
QPushButton:pressed {
color: rgb(100,100,100,150);
background-color: rgb(25, 25, 25, 150);
}
""")
self.AddExtSeries = QPushButton()
self.AddExtSeries.setToolTip('Add a series from a npy file')
im = QIcon('support_files/add.png')
self.AddExtSeries.setIcon(im)
self.AddExtSeries.setDisabled(True)
self.AddExtSeries.clicked.connect(self.addExtraSeries)
self.AddExtSeries.setStyleSheet("""
QPushButton {
border-width: 25px solid white;
border-radius: 0px;
color: rgb(180,180,180);
background-color: rgb(55, 55, 60, 0);
}
QPushButton:pressed {
color: rgb(100,100,100,150);
background-color: rgb(25, 25, 25, 150);
}
""")
self.SaveNPY = QPushButton()
self.SaveNPY.setToolTip('Save the series in a numpy array file (.npy)')
im = QIcon('support_files/save_npy.png')
self.SaveNPY.setIcon(im)
self.SaveNPY.setDisabled(True)
self.SaveNPY.clicked.connect(self.saveNPY)
self.SaveNPY.setStyleSheet("""
QPushButton {
border-width: 25px solid white;
border-radius: 0px;
color: rgb(180,180,180);
background-color: rgb(55, 55, 60, 0);
}
QPushButton:pressed {
color: rgb(100,100,100,150);
background-color: rgb(25, 25, 25, 150);
}
""")
# buttons to zoom and pan
self.zoombut = QPushButton()
self.zoombut.setCheckable(True)
self.zoombut.setEnabled(False)
im = QIcon('support_files/zoom_trans.png')
self.zoombut.setIcon(im)
self.zoombut.setDisabled(True)
self.zoombut.clicked.connect(self.zoom)
self.zoombut.setStyleSheet("""
QPushButton {
border-width: 25px solid white;
border-radius: 0px;
color: rgb(180,180,180);
background-color: rgb(55, 55, 60, 0);
}
QPushButton:checked {
color: rgb(100,100,100,150);
background-color: rgb(255, 128, 0);
}
""")
self.homebut = QPushButton()
self.homebut.setEnabled(False)
im = QIcon('support_files/home_trans.png')
self.homebut.setIcon(im)
self.homebut.setDisabled(True)
self.homebut.clicked.connect(self.home)
self.homebut.setStyleSheet("""
QPushButton {
border-width: 25px solid white;
border-radius: 0px;
color: rgb(180,180,180,50);
background-color: rgb(25, 25, 60, 0);
}
QPushButton:pressed {
color: rgb(100,100,100,150);
background-color: rgb(255, 128, 0);
}
""")
self.toolbar = NavigationToolbar(self.canvas, self)
self.toolbar.hide()
def implementGrid(self):
grid = QGridLayout()
grid.addWidget(self.canvas,0,0)
box1 = QVBoxLayout()
box1.addSpacing(30)
box1.addWidget(self.Export)
box1.addWidget(self.AddExtSeries)
box1.addWidget(self.SaveNPY)
box1.addStretch()
box1.addWidget(self.zoombut)
box1.addWidget(self.homebut)
box1.addWidget(QLabel())
box2 = QHBoxLayout()
box2.addStretch()
box2.addLayout(box1)
box2.addSpacing(15)
grid.addLayout(box2,0,0)
self.setLayout(grid)
def plotSeries(self, T, var, t0, t1, x, y):
self.Export.setEnabled(True)
self.AddExtSeries.setEnabled(True)
self.SaveNPY.setEnabled(True)
self.zoombut.setEnabled(True)
self.homebut.setEnabled(True)
self.new_series = []
self.new_series_labels = []
self.x, self.y = x, y
self.T = T
self.var = var
self.t0 = t0
self.t1 = t1
mask = (T>t0)*(T<t1)
self.mask = mask
self.varmin = np.min(var[mask])
self.varmax = np.max(var[mask])
self.varrange = self.varmax - self.varmin
self.ax.set_xlim(t0,t1)
self.ax.clear()
self.ax.grid('on')
self.ax.plot(T, var,'.-', color = (1, 128/255, 0), label = 'active output')
self.ax.set_ylim(max(-999,self.varmin - 0.1*self.varrange), min(999,self.varmax + 0.1*self.varrange))
self.ax.set_ylabel(self.label, fontweight = 'bold', fontsize = 9)
self.canvas.draw()
def exportGraph(self):
lab = 'x: %d, y: %d' % (self.x, self.y)
fn, _ = QFileDialog.getSaveFileName(self,"QFileDialog.getSaveFileName()","","All Files (*);;PNG Files (*.png)", options=QFileDialog.Options())
if len(fn) > 0:
f, a = plt.subplots(figsize = (15,4))
f.subplots_adjust(left = 0.1, right = 0.94, bottom = 0.1, top = 0.9)
a.plot(self.T, self.var,'.-', color = (1, 128/255, 0), label = lab)
a.grid('on')
a.set_ylabel(self.label, fontweight = 'bold', fontsize = 11)
a.set_xlim(self.t0,self.t1)
a.set_ylim(max(-999,self.varmin - 0.1*self.varrange), min(999,self.varmax + 0.1*self.varrange))
for i in range(len(self.new_series)):
series = self.new_series[i]
lab = self.new_series_labels[i]
T = np.array(series[:,0], dtype = 'datetime64')
V = series[:,1]
a.plot(T, V, '.-', label = lab)
a.set_ylim(max(-999,self.varmin - 0.1*self.varrange), min(999,self.varmax + 0.1*self.varrange))
a.legend(loc = 1)
f.savefig(fn)
f.clear()
def saveNPY(self):
fn, _ = QFileDialog.getSaveFileName(self,"QFileDialog.getSaveFileName()","","All Files (*);;Numpy Files (*.npy)", options=QFileDialog.Options())
if len(fn) > 0:
if fn[-4:] != '.npy': fn + '.npy'
T = self.T - np.timedelta64(3600,'s')
var = self.var
arr = np.empty((T.shape[0], 2))
arr[:,0] = T
arr[:,1] = var
np.save(fn, arr)
def addExtraSeries(self):
options = QFileDialog.Options()
fn, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","Numpy files (*.npy)", options=options)
if len(fn) > 0:
arr = np.load(fn)
if arr.shape[1] != 2:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Not a valid numpy file. This array does not have two columns.")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
else:
self.new_series.append(arr)
lab = fn.split('/')[-1][:-4].replace('_',' ')
self.new_series_labels.append(lab)
T = np.array(arr[:,0], dtype = 'datetime64') + np.timedelta64(3600,'s')
self.ax.plot(T, arr[:,1], '.-', label = lab, scalex = False, scaley = False)
self.ax.legend(loc = 1)
self.varmin = np.min([self.varmin, np.min(arr[:,1])])
self.varmax = np.max([self.varmax, np.max(arr[:,1])])
self.varrange = abs(self.varmax - self.varmin)
#self.ax.set_ylim(max(-999,self.varmin - 0.1*self.varrange), min(999,self.varmax + 0.1*self.varrange))
self.canvas.draw()
def home(self):
self.toolbar.home()
def zoom(self):
if self.zoombut.isChecked():
self.toolbar.zoom()
self.record_pressing_event = False
else:
self.toolbar.zoom(False)
###########################################################################################################################################################################################################
def plotVarMesh(x,y,ikle,var, label_str, title_str = '', path = None, min = 0, max = 1e9, ax = None, fig = None, showedges = True, showgrid = True):
# ------------------------------------------------------------------------------ #
# Plot the Mesh
xmin, xmax = np.min(x),np.max(x)
ymin, ymax = np.min(y),np.max(y)
if ax == None:
fig, ax = plt.subplots(figsize = (12,12))
plt.tight_layout()
ax.cla()
if showedges:
tc = ax.tripcolor(x, y, ikle-1, var, vmin = min, vmax = max, cmap = 'gist_earth', ec = 'white', linewidth = 0.025)
else:
tc = ax.tripcolor(x, y, ikle-1, var, vmin = min, vmax = max, cmap = 'gist_earth')
ax.axis('off')
ax.margins(2)
ax.set_xlim(xmin - 0.05*(xmax - xmin), xmax + 0.05*(xmax - xmin))
ax.set_ylim(ymin - 0.05*(ymax - ymin), ymax + 0.05*(ymax - ymin))
ax.set_aspect('equal')
if len(title_str) > 0:
ax.set_title(title_str, color = 'white')
if showgrid:
ax.axis('on')
ax.tick_params(axis='x', colors='white', labelsize = 7, top = True, bottom = False, labeltop = True)
ax.tick_params(axis='y', colors='white', labelsize = 7)
ax.spines['bottom'].set_color('white')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('white')
ax.set_facecolor((45/255, 45/255, 45/255))
ax.grid('on', color = 'white')
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="5%", pad=0.05)
cb = fig.colorbar(tc, orientation = 'horizontal', cax = cax)
cb.ax.set_title(label_str,size = 10, color = 'white')
cb.ax.tick_params(labelsize=8, color = 'white', labelcolor = 'white')
if path != None:
fig.savefig(path)
os.system('nomacs %s' % path)