forked from ZedThree/pyxpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatplotlib_widget.py
More file actions
144 lines (122 loc) · 5.05 KB
/
matplotlib_widget.py
File metadata and controls
144 lines (122 loc) · 5.05 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
#
# Initial code taken from http://stackoverflow.com/questions/6723527/getting-pyside-to-work-with-matplotlib
# Additional bits from https://gist.github.com/jfburkhart/2423179
#
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4'] = 'PySide'
from matplotlib.figure import Figure
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
from matplotlib.pyplot import setp
from PySide.QtGui import QVBoxLayout
class MatplotlibWidget():
def __init__(self, parent):
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.canvas.setParent(parent)
self.mpl_toolbar = NavigationToolbar(self.canvas, parent)
self.axes = self.figure.add_subplot(111)
self.grid_layout = QVBoxLayout()
self.grid_layout.addWidget(self.canvas)
self.grid_layout.addWidget(self.mpl_toolbar)
parent.setLayout(self.grid_layout)
def plot(self, *args):
"""
Make multiple plots
"""
nplots = len(args)
self.axes.clear()
self.figure.clear()
self.axes.grid(True)
for plotnum, p in enumerate(args, 1):
# For each plot
if plotnum == 1:
self.axes = self.figure.add_subplot(nplots, 1, plotnum)
ax = self.axes
else:
self.axes = self.figure.add_subplot(nplots, 1, plotnum, sharex=ax)
setp(self.axes.get_xticklabels(), visible=False)
try:
# Assume each p is a list of data items to be overplotted
for data in p:
# Plot data
#self.axes.plot(data.time, data.data)
label = data.desc
if label == "":
label = data.label
label += " " + data.source
time = data.time
if time is None:
if len(data.dim) != 1:
print(data.dim)
raise ValueError("Cannot plot '"+data.label+"' as it has too many dimensions")
time = data.dim[0].data
self.axes.plot(time, data.data, label=label)
if plotnum == 0:
self.axes.set_xlabel(p[0].dim[p[0].order].label)
self.axes.legend()
except TypeError:
# p not iterable, so just plot item
#self.axes.plot(p.time, p.data)
time = p.time.data
xlabel = p.dim[p.order].label
if time is None:
if len(p.dim) != 1:
raise ValueError("Cannot plot '"+p.label+"' as it has too many dimensions")
time = p.dim[0].data
xlabel = p.dim[0].label
data = p.data
# Check the size of the array
size = len(time)
if size > 10000:
fac = int(len(time) / 10000)
time = time[::fac]
data = data[::fac]
print("Warning: too many samples (%d). Down-sampling to %d points" % (size, len(time)))
self.axes.plot(time, data)
ylabel = p.desc
if ylabel == "":
ylabel = p.label
if p.units != "":
ylabel += " ("+p.units+")"
self.axes.set_ylabel(ylabel)
if plotnum == 0:
self.axes.set_xlabel(xlabel)
self.figure.subplots_adjust(left=0.08, right=0.98, top=0.95, bottom=0.07, hspace=0.001)
self.canvas.draw()
def plotxy(self, x, y):
"""
Plot one variable against another
"""
self.axes.clear()
self.figure.clear()
self.axes = self.figure.add_subplot(111)
self.figure.subplots_adjust(left=0.07, right=0.98, top=0.95, bottom=0.08)
self.axes.plot(x.data, y.data)
self.axes.set_xlabel(x.name)
self.axes.set_ylabel(y.name)
self.canvas.draw()
def contour(self, item):
if len(item.data.shape) != 2: # Must be 2D
print("Data must be 2 dimensional")
return
self.axes.clear()
self.figure.clear()
self.axes = self.figure.add_subplot(111)
self.figure.subplots_adjust(left=0.07, right=0.98, top=0.95, bottom=0.08)
self.axes.contour(item.data)
self.canvas.draw()
def contourf(self, item):
if len(item.data.shape) != 2: # Must be 2D
print("Data must be 2 dimensional")
return
self.axes.clear()
self.figure.clear()
self.axes = self.figure.add_subplot(111)
self.figure.subplots_adjust(left=0.07, right=0.98, top=0.95, bottom=0.08)
self.axes.contourf(item.data)
self.canvas.draw()