-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplot_wdg.py
486 lines (368 loc) · 12.5 KB
/
plot_wdg.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# -*- coding: utf-8 -*-
"""
/****************************************************************************
Name : GEM Modellers Toolkit plugin (GEM-MT)
Description : Analysing and Processing Earthquake Catalogue Data
Date : Jun 21, 2012
copyright : (C) 2012 by Giuseppe Sucameli (Faunalia)
email : [email protected]
****************************************************************************/
/****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
****************************************************************************/
"""
from qgis.PyQt.QtWidgets import QDialog, QVBoxLayout, QApplication
from qgis.PyQt.QtCore import Qt, QVariant
from qgis.PyQt.QtGui import QCursor
# Matplotlib Figure object
from matplotlib.figure import Figure
from datetime import datetime, date
from matplotlib.dates import date2num, num2date, YearLocator, MonthLocator, DayLocator, DateFormatter
from matplotlib.lines import Line2D
# import the Qt4Agg FigureCanvas object, that binds Figure to
# Qt4Agg backend. It also inherits from QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
class PlotWdg(FigureCanvasQTAgg):
"""Class to represent the FigureCanvas widget"""
def __init__(self, data=None, labels=None, title=None, props=None):
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
# initialize the canvas where the Figure renders into
FigureCanvasQTAgg.__init__(self, self.fig)
self._dirty = False
self.collections = []
if not data: data = [None]
self.setData( *data )
if not labels: labels = [None]
self.setLabels( *labels )
self.setTitle( title )
self.props = props if isinstance(props, dict) else {}
yscale = self.props.get('yscale', None)
if yscale:
self.axes.set_yscale( yscale )
def itemAt(self, index):
if index >= len(self.x):
return None
return (self.x[index] if self.x else None, self.y[index] if self.y else None)
def delete(self):
self._clear()
# unset delete function
self.delete = lambda: None
def __del__(self):
self.delete()
def deleteLater(self, *args):
self.delete()
return FigureCanvasQTAgg.deleteLater(self, *args)
def destroy(self, *args):
self.delete()
return FigureCanvasQTAgg.destroy(self, *args)
def setDirty(self, val):
self._dirty = val
def showEvent(self, event):
if self._dirty:
self.refreshData()
return FigureCanvasQTAgg.showEvent(self, event)
def refreshData(self):
# remove the old stuff
self._clear()
# plot the new data
self._plot()
# update axis limits
self.axes.relim() # it doesn't shrink until removing all the objects on the axis
# re-draw
self.draw()
# unset the dirty flag
self._dirty = False
def setData(self, x, y=None, info=None):
self.x = x if x is not None else []
self.y = y if y is not None else []
self.info = info if info is not None else []
self._dirty = True
def getTitle(self):
return self.axes.get_title()
def setTitle(self, title, *args, **kwargs):
self.axes.set_title( title or "", *args, **kwargs )
self.draw()
def getLabels(self):
return self.axes.get_xlabel(), self.axes.get_ylabel()
def setLabels(self, xLabel=None, yLabel=None, *args, **kwargs):
self.axes.set_xlabel( xLabel or "", *args, **kwargs )
self.axes.set_ylabel( yLabel or "", *args, **kwargs )
self.draw()
def getLimits(self):
xlim = self.axes.get_xlim()
is_x_date = isinstance(self.x[0], (datetime, date)) if len(self.x) > 0 else False
if is_x_date:
xlim = num2date(xlim)
ylim = self.axes.get_ylim()
is_y_date = isinstance(self.y[0], (datetime, date)) if self.y is not None and len(self.y) > 0 else False
if is_y_date:
ylim = num2date(ylim)
return xlim, ylim
def setLimits(self, xlim=None, ylim=None):
""" update the chart limits """
if xlim is not None:
self.axes.set_xlim(xlim)
if ylim is not None:
self.axes.set_ylim(ylim)
self.draw()
def displayGrids(self, hgrid=False, vgrid=False):
self.axes.xaxis.grid(vgrid, 'major')
self.axes.yaxis.grid(hgrid, 'major')
self.draw()
def _removeItem(self, item):
try:
self.collections.remove( item )
except ValueError:
pass
try:
if isinstance(item, (list, tuple, set)):
for i in item:
i.remove()
else:
item.remove()
except (ValueError, AttributeError):
pass
def _clear(self):
for item in self.collections:
self._removeItem( item )
self.collections = []
def _plot(self):
# convert values, then create the plot
x = map(PlotWdg._valueFromQVariant, self.x)
y = map(PlotWdg._valueFromQVariant, self.y)
items = self._callPlotFunc('plot', x, y)
self.collections.append( items )
def _callPlotFunc(self, plotfunc, x, y=None, *args, **kwargs):
is_x_date = isinstance(x[0], (datetime, date)) if len(x) > 0 else False
is_y_date = isinstance(y[0], (datetime, date)) if y is not None and len(y) > 0 else False
if is_x_date:
self._setAxisDateFormatter( self.axes.xaxis, x )
x = date2num(x)
if is_y_date:
self._setAxisDateFormatter( self.axes.yaxis, y )
y = date2num(y)
if y is not None:
items = getattr(self.axes, plotfunc)(x, y, *args, **kwargs)
else:
items = getattr(self.axes, plotfunc)(x, *args, **kwargs)
if is_x_date:
self.fig.autofmt_xdate()
#if is_y_date:
# self.fig.autofmt_ydate()
return items
@classmethod
def _setAxisDateFormatter(self, axis, data):
timedelta = max(data) - min(data)
if timedelta.days > 365*5:
axis.set_major_formatter( DateFormatter('%Y') )
#axis.set_major_locator( YearLocator() )
#axis.set_minor_locator( MonthLocator() )
#bins = timedelta.days * 4 / 356 # four bins for a year
elif timedelta.days > 30*5:
axis.set_major_formatter( DateFormatter('%Y-%m') )
#axis.set_major_locator( MonthLocator() )
#axis.set_minor_locator( DayLocator() )
#bins = timedelta.days * 4 / 30 # four bins for a month
else:
axis.set_major_formatter( DateFormatter('%Y-%m-%d') )
#axis.set_major_locator( DayLocator() )
#axis.set_minor_locator( HourLocator() )
#bins = timedelta.days * 4 # four bins for a day
@staticmethod
def _valueFromQVariant(val):
""" function to convert values to proper types """
if not isinstance(val, QVariant):
return val
if val.type() == QVariant.Int:
return int(val)
elif val.type() == QVariant.Double:
return float(val)
elif val.type() == QVariant.Date:
return val.toDate().toPyDate()
elif val.type() == QVariant.DateTime:
return val.toDateTime().toPyDateTime()
# try to convert the value to a date
s = str(val)
try:
return datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
except ValueError:
pass
try:
return datetime.strptime(s, '%Y-%m-%d')
except ValueError:
pass
v, ok = val
if ok: return v
v, ok = val
if ok: return v
v = val.toDateTime()
if v.isValid(): return v.toPyDateTime()
v = val.toDate()
if v.isValid(): return v.toPyDate()
return str(s)
class HistogramPlotWdg(PlotWdg):
def __init__(self, *args, **kwargs):
PlotWdg.__init__(self, *args, **kwargs)
def _plot(self):
# convert values, then create the plot
x = map(PlotWdg._valueFromQVariant, self.x)
items = self._callPlotFunc('hist', x, bins=50)
self.collections.append( items )
class ScatterPlotWdg(PlotWdg):
def __init__(self, *args, **kwargs):
PlotWdg.__init__(self, *args, **kwargs)
def _plot(self):
# convert values, then create the plot
x = map(PlotWdg._valueFromQVariant, self.x)
y = map(PlotWdg._valueFromQVariant, self.y)
items = self._callPlotFunc('scatter', x, y)
self.collections.append( items )
class PlotDlg(QDialog):
def __init__(self, parent, *args, **kwargs):
QDialog.__init__(self, parent, Qt.Window)
self.setWindowTitle("Plot dialog")
layout = QVBoxLayout(self)
self.plot = self.createPlot(*args, **kwargs)
layout.addWidget(self.plot)
self.nav = self.createToolBar()
layout.addWidget(self.nav)
def enterEvent(self, event):
self.nav.set_cursor( NavigationToolbar.Cursor.POINTER )
return QDialog.enterEvent(self, event)
def leaveEvent(self, event):
self.nav.unset_cursor()
return QDialog.leaveEvent(self, event)
def createPlot(self, *args, **kwargs):
return PlotWdg(*args, **kwargs)
def createToolBar(self):
return NavigationToolbar(self.plot, self)
def refresh(self):
# query for refresh
self.plot.setDirty(True)
if self.isVisible():
# refresh if it's already visible
self.plot.refreshData()
def setData(self, x, y=None, info=None):
self.plot.setData(x, y, info)
def setTitle(self, title):
self.plot.setTitle(title)
def setLabels(self, xLabel, yLabel):
self.plot.setLabels(xLabel, yLabel)
class HistogramPlotDlg(PlotDlg):
def __init__(self, *args, **kwargs):
PlotDlg.__init__(self, *args, **kwargs)
def createPlot(self, *args, **kwargs):
return HistogramPlotWdg(*args, **kwargs)
class ScatterPlotDlg(PlotDlg):
def __init__(self, *args, **kwargs):
PlotDlg.__init__(self, *args, **kwargs)
def createPlot(self, *args, **kwargs):
return ScatterPlotWdg(*args, **kwargs)
# import the NavigationToolbar Qt4Agg widget
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT
class NavigationToolbar(NavigationToolbar2QT):
def __init__(self, *args, **kwargs):
NavigationToolbar2QT.__init__(self, *args, **kwargs)
self.init_buttons()
self.panAction.setCheckable(True)
self.zoomAction.setCheckable(True)
# remove the subplots action
self.removeAction( self.subplotsAction )
def configure_subplots(self, *args):
pass # do nothing
class Cursor:
# cursors defined in backend_bases (from matplotlib source code)
HAND, POINTER, SELECT_REGION, MOVE = range(4)
@classmethod
def toQCursor(self, cursor):
if cursor == self.MOVE:
n = Qt.SizeAllCursor
elif cursor == self.HAND:
n = Qt.PointingHandCursor
elif cursor == self.SELECT_REGION:
n = Qt.CrossCursor
else:#if cursor == self.POINTER:
n = Qt.ArrowCursor
return QCursor( n )
def set_cursor(self, cursor):
if cursor != self._lastCursor:
self.unset_cursor()
QApplication.setOverrideCursor( NavigationToolbar.Cursor.toQCursor(cursor) )
self._lastCursor = cursor
def unset_cursor(self):
if self._lastCursor:
QApplication.restoreOverrideCursor()
self._lastCursor = None
def init_buttons(self):
self.homeAction = self.panAction = self.zoomAction = self.subplotsAction = None
for a in self.actions():
if a.text() == 'Home':
self.homeAction = a
elif a.text() == 'Pan':
self.panAction = a
elif a.text() == 'Zoom':
self.zoomAction = a
elif a.text() == 'Subplots':
self.subplotsAction = a
def resetActionsState(self, skip=None):
# reset the buttons state
for a in self.actions():
if skip and a == skip:
continue
a.setChecked( False )
def pan( self, *args ):
self.resetActionsState( self.panAction )
NavigationToolbar2QT.pan( self, *args )
def zoom( self, *args ):
self.resetActionsState( self.zoomAction )
NavigationToolbar2QT.zoom( self, *args )
class ClippedLine2D(Line2D):
"""
Clip the xlimits to the axes view limits
"""
def __init__(self, *args, **kwargs):
Line2D.__init__(self, *args, **kwargs)
def draw(self, renderer):
x, y = self.get_data()
if len(x) == 2 or len(y) == 2:
xlim = self.axes.get_xlim()
ylim = self.axes.get_ylim()
x0, y0 = x[0], y[0]
x1, y1 = x[1], y[1]
if x0 == x1: # vertical
x, y = (x0, x0), ylim
elif y0 == y1: # horizontal
x, y = xlim, (y0, y0)
else:
# coeff != 0
coeff = float(y1 - y0) / (x1 - x0)
minx = (ylim[0] - y0) / coeff + x0
maxx = (ylim[1] - y0) / coeff + x0
miny = coeff * (xlim[0] - x0) + y0
maxy = coeff * (xlim[1] - x0) + y0
if coeff > 0:
x = max(minx, xlim[0]), min(maxx, xlim[1])
y = max(miny, ylim[0]), min(maxy, ylim[1])
else:
x = max(maxx, xlim[0]), min(minx, xlim[1])
y = min(miny, ylim[1]), max(maxy, ylim[0])
self.set_data(x, y)
Line2D.draw(self, renderer)
if __name__ == "__main__":
# for command-line arguments
import sys
# Create the GUI application
app = QApplication(sys.argv)
# show a histogram plot
HistogramPlotWdg( [[1,2,1,1,4,3,4,5]], ["x", "y"] ).show()
# show a scatter plot
ScatterPlotWdg( data=([1,2,3,4,5],[10,9,7,4,0]), labels=("x", "y"), title="ScatterPlot" ).show()
# start the Qt main loop execution, exiting from this script
# with the same return code of Qt application
sys.exit(app.exec_())