-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_gallery.py
256 lines (192 loc) · 7.95 KB
/
plot_gallery.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
"""
Distributed under the terms of the BSD 3-Clause License.
The full license is in the file LICENSE, distributed with this software.
Author: Jun Zhu
"""
from foamgraph.backend.QtWidgets import QFrame, QGridLayout
from foamgraph import (
LiveWindow, FColor, mkQApp, GraphView, TimedGraphView
)
from consumer import Consumer
app = mkQApp()
class ShadedPlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Shaded plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self._plot1 = self.addCurvePlot(pen=FColor.mkPen('Gray', width=2))
self._plot2 = self.addShadedPlot(pen=FColor.mkPen('DodgerBlue'))
def updateF(self, data):
"""Override."""
data = data['line']
self._plot1.setData(data['x'], data['y3'], data['y3_err'])
self._plot2.setData(data['x'], data['y1'], data['y2'])
class ScatterPlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Scatter plot (aspect ratio locked)')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self._plot1 = self.addScatterPlot(label="Data1", symbol="d", size=9)
self._plot2 = self.addScatterPlot(
label="Data2", brush=FColor.mkBrush('Purple', alpha=50), size=7)
self._plot3 = self.addScatterPlot(
label="Data3", brush=None, pen=FColor.mkPen('Brown', alpha=100), symbol="s")
self._plot4 = self.addScatterPlot(
label="Data4", pen=FColor.mkPen('k', alpha=150), symbol="+")
self.addLegend()
self.setAspectLocked(True)
def updateF(self, data):
"""Override."""
data = data['scatter']
self._plot1.setData(data['x1'], data['y1'])
self._plot2.setData(data['x2'], data['y2'])
self._plot3.setData(data['x3'], data['y3'])
self._plot4.setData(data['x4'], data['y4'])
class BarPlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Bar plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self.addLegend()
self._plot1 = self.addBarPlot(
width=0.9, label="bar1", brush=FColor.mkBrush("b"))
self._plot2 = self.addBarPlot(
width=0.9, label="bar2", brush=FColor.mkBrush('DodgerBlue'))
self._plot3 = self.addBarPlot(
width=0.9, label="bar3", brush=FColor.mkBrush('ForestGreen'))
self._stack_vertical = False
def updateF(self, data):
"""Override."""
data = data['bar']
if self._stack_vertical ^ data['stack_vertical']:
self._stack_vertical = data['stack_vertical']
if self._stack_vertical:
self.setBarPlotStackOrientation('v')
else:
self.setBarPlotStackOrientation('h')
self._plot1.setData(data['x'], data['y1'])
self._plot2.setData(data['x'], data['y2'])
self._plot3.setData(data['x'], data['y3'])
class ErrorbarPlot(TimedGraphView):
def __init__(self, *, parent=None):
super().__init__(1000, parent=parent)
self.setTitle('Timed error-bar plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self._plot1 = self.addErrorbarPlot(pen=FColor.mkPen('ForestGreen'))
self._plot2 = self.addScatterPlot(brush=FColor.mkBrush('DodgerBlue'), size=6)
def refresh(self):
"""Override."""
data = self._data['errorbar']
self._plot1.setData(data['x'], data['y'], data['y_min'], data['y_max'])
self._plot2.setData(data['x'], data['y'])
class MultiLinePlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Multi-line plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self.addLegend() # add legend before plots
self._plot1 = self.addCurvePlot(
label='Line A', pen=FColor.mkPen('k', width=1))
self._plot2 = self.addCurvePlot(
label='Line B', pen=FColor.mkPen('b', width=2))
self._plot3 = self.addCurvePlot(
label='Line C', pen=FColor.mkPen('r', width=3))
def updateF(self, data):
"""Override."""
data = data['multi-line']
self._plot1.setData(data['x'], data['y1'])
self._plot2.setData(data['x'], data['y2'])
self._plot3.setData(data['x'], data['y3'])
class DoubleYPlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Double-y plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)", y2="y2 (arb. u.)")
self._plot = self.addCurvePlot(pen=FColor.mkPen('Brown'))
self._plot1 = self.addScatterPlot(
label="Data", symbol='o', pen=FColor.mkPen('Brown'))
self._plot2 = self.addBarPlot(
label="Count", y2=True, brush=FColor.mkBrush('Silver', alpha=150))
self.addLegend()
def updateF(self, data):
"""Override."""
data = data['double-y']
self._plot.setData(data['x'], data['y'])
self._plot1.setData(data['x'], data['y'])
self._plot2.setData(data['x'], data['y2'])
class LinePlotWithAnnotation(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Line plot with peak annotation')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self._plot = self.addCurvePlot(simple=True, pen=FColor.mkPen('k'))
self._annotation = self.addAnnotation()
def updateF(self, data):
"""Override."""
data = data['multi-peak']
x, y, peaks = data['x'], data['y'], data['peaks']
self._plot.setData(x, y)
self._annotation.setData(x[peaks], y[peaks], annotations=x[peaks])
class CandlestickPlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Candlestick plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self._plot = self.addCandlestickPlot()
def updateF(self, data):
"""Override."""
data = data['candlestick']
self._plot.setData(data['x'], data['y_start'], data['y_end'],
data['y_min'], data['y_max'])
class StemPlot(GraphView):
def __init__(self, *, parent=None):
super().__init__(parent=parent)
self.setTitle('Stem plot')
self.setXYLabels("x (arb. u.)", "y (arb. u.)")
self._plot1 = self.addStemPlot(
label="Data1", symbol="s", pen=FColor.mkPen('DodgerBlue'))
self._plot2 = self.addStemPlot(
label="Data2", symbol="o", brush=FColor.mkBrush('DarkOrange'))
self.addLegend()
def updateF(self, data):
"""Override."""
data = data['stem']
self._plot1.setData(data['x'], data['y1'])
self._plot2.setData(data['x'], data['y2'])
class PlotGalleryWindow(LiveWindow):
def __init__(self):
super().__init__("Plot gallery")
self._plots = [
ShadedPlot(parent=self),
ScatterPlot(parent=self),
BarPlot(parent=self),
ErrorbarPlot(parent=self),
MultiLinePlot(parent=self),
DoubleYPlot(parent=self),
LinePlotWithAnnotation(parent=self),
CandlestickPlot(parent=self),
StemPlot(parent=self)
]
self.init()
def initUI(self):
"""Override."""
layout = QGridLayout()
rows, cols = 3, 3
for i in range(rows):
for j in range(cols):
layout.addWidget(self._plots[cols * i + j], i, j)
self._cw = QFrame()
self._cw.setLayout(layout)
self.setCentralWidget(self._cw)
w, h = 1440, 1080
self.resize(w, h)
self.setMinimumSize(int(0.6 * w), int(0.6 * h))
def initConnections(self):
"""Override."""
...
if __name__ == "__main__":
gui = PlotGalleryWindow()
consumer = Consumer(gui.queue)
gui.start()
consumer.start()
app.exec()