File qt_plot.py changed (mode: 100644) (index 2ba2085..8424b23) |
3 |
3 |
|
|
4 |
4 |
import pyqtgraph as pg |
import pyqtgraph as pg |
5 |
5 |
from pyqtgraph.Qt import QtGui |
from pyqtgraph.Qt import QtGui |
|
6 |
|
from pyqtgraph.Qt import QtWidgets |
|
7 |
|
from pyqtgraph.Qt import QtCore |
6 |
8 |
|
|
|
9 |
|
import numpy as np |
7 |
10 |
|
|
8 |
|
# snad pri vykreslování argy, kvargy nevádí |
|
9 |
11 |
def qt_gui_plot_2d(sample_box, space='R', *args, **kwargs): |
def qt_gui_plot_2d(sample_box, space='R', *args, **kwargs): |
10 |
|
app = pg.mkQApp() |
|
11 |
|
### Define a top-level widget to hold everything |
|
12 |
|
title = space + " space plot" |
|
13 |
|
w = QtGui.QWidget() |
|
14 |
|
|
|
15 |
|
### Create some widgets to be placed inside |
|
16 |
|
btn = QtGui.QPushButton('press me') |
|
17 |
|
text = QtGui.QLineEdit('enter text') |
|
18 |
|
listw = QtGui.QListWidget() |
|
19 |
|
|
|
20 |
|
# zkusím prostě generovať |
|
21 |
|
plotWidget = pg.PlotWidget() |
|
22 |
|
plot = plot_widget_2d(sample_box, plotWidget, space, *args, **kwargs) |
|
23 |
|
|
|
24 |
|
## Create a grid layout to manage the widgets size and position |
|
25 |
|
layout = QtGui.QGridLayout() |
|
26 |
|
w.setLayout(layout) |
|
27 |
|
|
|
28 |
|
## Add widgets to the layout in their proper positions |
|
29 |
|
layout.addWidget(btn, 0, 0) # button goes in upper-left |
|
30 |
|
layout.addWidget(text, 1, 0) # text edit goes in middle-left |
|
31 |
|
layout.addWidget(listw, 2, 0) # list widget goes in bottom-left |
|
32 |
|
layout.addWidget(plot, 0, 1, 3, 1) # plot goes on right side, spanning 3 rows |
|
33 |
|
|
|
34 |
|
## Display the widget as a new window |
|
35 |
|
w.show() |
|
36 |
|
|
|
37 |
|
## Start the Qt event loop |
|
38 |
|
app.exec_() |
|
|
12 |
|
QtGuiPlot2D(sample_box, space, *args, **kwargs) |
39 |
13 |
|
|
40 |
14 |
|
|
41 |
|
# snad pri vykreslování argy, kvargy nevádí |
|
42 |
|
def plot_widget_2d(sample_box, plotWidget, space='R', *args, **kwargs): |
|
43 |
|
plotWidget.setBackground('w') |
|
44 |
|
|
|
45 |
|
x, y = (*getattr(sample_box.failure_samples, space).T,) |
|
46 |
|
plotWidget.plot(x, y, pen=None, symbol='x', symbolPen='r', name='Failures') # symbolBrush=0.2, |
|
47 |
|
|
|
48 |
|
x, y = (*getattr(sample_box.success_samples, space).T,) |
|
49 |
|
plotWidget.plot(x, y, pen=None, symbol='+', symbolPen='g', name='Successes') |
|
50 |
|
|
|
51 |
|
# ne všichni majó definované hranice |
|
52 |
|
try: |
|
53 |
|
bounds = sample_box.get_2D_boundary() |
|
54 |
|
|
|
55 |
|
for bound in bounds: |
|
56 |
|
plotWidget.plot(*getattr(bound, space).T, pen='b') |
|
57 |
|
except AttributeError: |
|
58 |
|
pass #print("čo sa děje?") |
|
59 |
|
|
|
60 |
|
return plotWidget |
|
|
15 |
|
class QtGuiPlot2D: |
|
16 |
|
# snad pri vykreslování argy, kvargy nevádí |
|
17 |
|
def __init__(self, sample_box, space='R', *args, **kwargs): |
|
18 |
|
self.sample_box = sample_box |
|
19 |
|
self.space = space |
|
20 |
|
|
|
21 |
|
app = pg.mkQApp() |
|
22 |
|
|
|
23 |
|
|
|
24 |
|
### Define a top-level widget to hold everything |
|
25 |
|
w = QtGui.QWidget() |
|
26 |
|
w.setWindowTitle(sample_box.gm_signature + " plot") |
|
27 |
|
|
|
28 |
|
### Create some widgets to be placed inside |
|
29 |
|
self.combo_space = pg.ComboBox(items=['R', 'Rn', 'P', 'GK', 'G', 'U'], default=space) |
|
30 |
|
self.combo_space.activated[str].connect(self.change_space) |
|
31 |
|
|
|
32 |
|
|
|
33 |
|
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal) |
|
34 |
|
self.slider.setValue(100) |
|
35 |
|
self.slider.valueChanged.connect(self.slice_plot_data) |
|
36 |
|
|
|
37 |
|
self.label_nsim = pg.ValueLabel() |
|
38 |
|
self.label_nsim.setValue(sample_box.nsim) |
|
39 |
|
|
|
40 |
|
|
|
41 |
|
btn = QtGui.QPushButton('run box') |
|
42 |
|
btn.clicked.connect(lambda:self.slice_plot_data(self.sample_box())) |
|
43 |
|
|
|
44 |
|
#text = QtGui.QLineEdit('enter text') |
|
45 |
|
#listw = QtGui.QListWidget() |
|
46 |
|
|
|
47 |
|
# zkusím prostě generovať |
|
48 |
|
self.plotWidget = pg.PlotWidget() |
|
49 |
|
self.plotWidget.setBackground('w') |
|
50 |
|
self.plot_widget_2d() |
|
51 |
|
|
|
52 |
|
## Create a grid layout to manage the widgets size and position |
|
53 |
|
layout = QtGui.QGridLayout() |
|
54 |
|
w.setLayout(layout) |
|
55 |
|
|
|
56 |
|
## Add widgets to the layout in their proper positions |
|
57 |
|
layout.addWidget(self.combo_space, 0, 0) |
|
58 |
|
layout.addWidget(self.slider, 0, 1) |
|
59 |
|
layout.addWidget(self.label_nsim, 0, 2) |
|
60 |
|
layout.addWidget(btn, 0, 3) |
|
61 |
|
layout.addWidget(self.plotWidget, 1, 0, 1, 4) |
|
62 |
|
|
|
63 |
|
## Display the widget as a new window |
|
64 |
|
w.show() |
|
65 |
|
|
|
66 |
|
## Start the Qt event loop |
|
67 |
|
app.exec_() |
61 |
68 |
|
|
62 |
|
# |
|
63 |
|
# |
|
64 |
|
#import pyqtgraph as pg |
|
65 |
|
#import numpy as np |
|
66 |
|
#x = np.arange(1000) |
|
67 |
|
#y = np.random.normal(size=(3, 1000)) |
|
68 |
|
#plotWidget = pg.plot(title="Three plot curves") |
|
69 |
|
#for i in range(3): |
|
70 |
|
# plotWidget.plot(x, y[i], pen=(i,3)) ## setting pen=(i,3) automaticaly creates three different-colored pens |
|
71 |
|
# |
|
72 |
|
# |
|
73 |
|
# |
|
74 |
|
# |
|
75 |
|
# |
|
76 |
|
#from PyQt5 import QtGui # (the example applies equally well to PySide) |
|
77 |
|
#import pyqtgraph as pg |
|
78 |
|
# |
|
79 |
|
### Always start by initializing Qt (only once per application) |
|
80 |
|
#app = QtGui.QApplication([]) |
|
81 |
|
# |
|
82 |
|
### Define a top-level widget to hold everything |
|
83 |
|
#w = QtGui.QWidget() |
|
84 |
|
# |
|
85 |
|
### Create some widgets to be placed inside |
|
86 |
|
#btn = QtGui.QPushButton('press me') |
|
87 |
|
#text = QtGui.QLineEdit('enter text') |
|
88 |
|
#listw = QtGui.QListWidget() |
|
89 |
|
#plot = pg.PlotWidget() |
|
90 |
|
# |
|
91 |
|
### Create a grid layout to manage the widgets size and position |
|
92 |
|
#layout = QtGui.QGridLayout() |
|
93 |
|
#w.setLayout(layout) |
|
94 |
|
# |
|
95 |
|
### Add widgets to the layout in their proper positions |
|
96 |
|
#layout.addWidget(btn, 0, 0) # button goes in upper-left |
|
97 |
|
#layout.addWidget(text, 1, 0) # text edit goes in middle-left |
|
98 |
|
#layout.addWidget(listw, 2, 0) # list widget goes in bottom-left |
|
99 |
|
#layout.addWidget(plot, 0, 1, 3, 1) # plot goes on right side, spanning 3 rows |
|
100 |
|
# |
|
101 |
|
### Display the widget as a new window |
|
102 |
|
#w.show() |
|
103 |
|
# |
|
104 |
|
### Start the Qt event loop |
|
105 |
|
#app.exec_() |
|
106 |
69 |
|
|
107 |
|
#print("Ӟеч-а бур-а?") |
|
|
70 |
|
def plot_widget_2d(self): |
|
71 |
|
self.plotWidget.clear() |
|
72 |
|
# Kružničky chcete? |
|
73 |
|
# Кружочки ннада? |
|
74 |
|
if self.space in ('Rn', 'G'): |
|
75 |
|
nrod = 200 |
|
76 |
|
phi = np.linspace(0, 6.283185307, nrod, endpoint=True) |
|
77 |
|
for r in range(5): |
|
78 |
|
bound_x = r * np.cos(phi) |
|
79 |
|
bound_y = r * np.sin(phi) |
|
80 |
|
pen = pg.mkPen(color='k', width=6-r) |
|
81 |
|
self.plotWidget.plot(bound_x, bound_y, pen=pen) |
|
82 |
|
elif self.space in ('P', 'U'): |
|
83 |
|
self.plotWidget.plot((0,0,1,1,0), (0,1,1,0,0), pen='k') |
|
84 |
|
|
|
85 |
|
x, y = (*getattr(self.sample_box.failure_samples, self.space).T,) |
|
86 |
|
self.failures = self.plotWidget.plot(x, y, pen=None, symbol='x', symbolPen='r', name='Failures') # symbolBrush=0.2, |
|
87 |
|
|
|
88 |
|
x, y = (*getattr(self.sample_box.success_samples, self.space).T,) |
|
89 |
|
self.successes = self.plotWidget.plot(x, y, pen=None, symbol='+', symbolPen='g', name='Successes') |
|
90 |
|
|
|
91 |
|
# ne všichni majó definované hranice |
|
92 |
|
try: |
|
93 |
|
bounds = self.sample_box.get_2D_boundary() |
|
94 |
|
|
|
95 |
|
for bound in bounds: |
|
96 |
|
self.plotWidget.plot(*getattr(bound, self.space).T, pen='b') |
|
97 |
|
except AttributeError: |
|
98 |
|
pass #print("čo sa děje?") |
|
99 |
|
|
|
100 |
|
# малы со кулэ? |
|
101 |
|
return self.plotWidget |
|
102 |
|
|
|
103 |
|
|
|
104 |
|
def slice_plot_data(self, *args): |
|
105 |
|
self.slider.value() |
|
106 |
|
nsim = round(self.sample_box.nsim * self.slider.value()/100) |
|
107 |
|
self.label_nsim.setValue(nsim) |
|
108 |
|
|
|
109 |
|
# Update the data |
|
110 |
|
x, y = (*getattr(self.sample_box[:nsim].failure_samples, self.space).T,) |
|
111 |
|
self.failures.setData(x, y) |
|
112 |
|
|
|
113 |
|
x, y = (*getattr(self.sample_box[:nsim].success_samples, self.space).T,) |
|
114 |
|
self.successes.setData(x, y) |
108 |
115 |
|
|
109 |
|
#from PyQt5 import QtWidgets, QtCore |
|
110 |
|
#from pyqtgraph import PlotWidget, plot |
|
111 |
|
#import pyqtgraph as pg |
|
112 |
|
##import sys # We need sys so that we can pass argv to QApplication |
|
113 |
|
#import os |
|
114 |
|
#from random import randint |
|
115 |
|
# |
|
116 |
|
#class MainWindow(QtWidgets.QMainWindow): |
|
117 |
|
# |
|
118 |
|
# def __init__(self, *args, **kwargs): |
|
119 |
|
# super(MainWindow, self).__init__(*args, **kwargs) |
|
120 |
|
# |
|
121 |
|
# self.graphWidget = pg.PlotWidget() |
|
122 |
|
# self.setCentralWidget(self.graphWidget) |
|
123 |
|
# |
|
124 |
|
# self.x = list(range(100)) # 100 time points |
|
125 |
|
# self.y = [randint(0,100) for _ in range(100)] # 100 data points |
|
126 |
|
# |
|
127 |
|
# self.graphWidget.setBackground('w') |
|
128 |
|
# |
|
129 |
|
# pen = pg.mkPen(color=(255, 0, 0)) |
|
130 |
|
# self.data_line = self.graphWidget.plot(self.x, self.y, pen=pen) |
|
131 |
|
# |
|
132 |
|
# # ... init continued ... |
|
133 |
|
# self.timer = QtCore.QTimer() |
|
134 |
|
# self.timer.setInterval(50) |
|
135 |
|
# self.timer.timeout.connect(self.update_plot_data) |
|
136 |
|
# self.timer.start() |
|
137 |
|
# |
|
138 |
|
# def update_plot_data(self): |
|
139 |
|
# |
|
140 |
|
# self.x = self.x[1:] # Remove the first y element. |
|
141 |
|
# self.x.append(self.x[-1] + 1) # Add a new value 1 higher than the last. |
|
142 |
|
# |
|
143 |
|
# self.y = self.y[1:] # Remove the first |
|
144 |
|
# self.y.append( randint(0,100)) # Add a new random value. |
|
145 |
|
# |
|
146 |
|
# self.data_line.setData(self.x, self.y) # Update the data |
|
147 |
|
# |
|
148 |
|
#app = QtWidgets.QApplication([]) |
|
149 |
|
#w = MainWindow() |
|
150 |
|
#w.show() |
|
151 |
|
#app.exec_() |
|
152 |
|
#print("konec!") |
|
|
116 |
|
|
|
117 |
|
def change_space(self, space): |
|
118 |
|
self.space = space |
|
119 |
|
self.plot_widget_2d() |
153 |
120 |
|
|
154 |
|
#from PyQt5 import QtWidgets, QtCore |
|
155 |
|
#from pyqtgraph import PlotWidget, plot |
|
156 |
|
#import pyqtgraph as pg |
|
157 |
|
#import sys # We need sys so that we can pass argv to QApplication |
|
158 |
|
#import os |
|
159 |
|
# |
|
160 |
|
#class MainWindow(QtWidgets.QMainWindow): |
|
161 |
|
# |
|
162 |
|
# def __init__(self, *args, **kwargs): |
|
163 |
|
# super(MainWindow, self).__init__(*args, **kwargs) |
|
164 |
|
# |
|
165 |
|
# self.graphWidget = pg.PlotWidget() |
|
166 |
|
# self.setCentralWidget(self.graphWidget) |
|
167 |
|
# |
|
168 |
|
# hour = [1,2,3,4,5,6,7,8,9,10] |
|
169 |
|
# temperature_1 = [30,32,34,32,33,31,29,32,35,45] |
|
170 |
|
# temperature_2 = [50,35,44,22,38,32,27,38,32,44] |
|
171 |
|
# |
|
172 |
|
# #Add Background colour to white |
|
173 |
|
# self.graphWidget.setBackground('w') |
|
174 |
|
# #Add Title |
|
175 |
|
# #self.graphWidget.setTitle("Your Title Here", color='blue', size=30) |
|
176 |
|
# #Add Axis Labels |
|
177 |
|
# self.graphWidget.setLabel('left', 'Temperature (°C)', color='red', size=30) |
|
178 |
|
# self.graphWidget.setLabel('bottom', 'Hour (H)', color='red', size=30) |
|
179 |
|
# #Add legend |
|
180 |
|
# self.graphWidget.addLegend() |
|
181 |
|
# #Add grid |
|
182 |
|
# self.graphWidget.showGrid(x=True, y=True) |
|
183 |
|
# #Set Range |
|
184 |
|
# self.graphWidget.setXRange(0, 10, padding=0) |
|
185 |
|
# self.graphWidget.setYRange(20, 55, padding=0) |
|
186 |
|
# |
|
187 |
|
# self.plot(hour, temperature_1, "Sensor1", 'r') |
|
188 |
|
# self.plot(hour, temperature_2, "Sensor2", 'b') |
|
189 |
|
# |
|
190 |
|
# def plot(self, x, y, plotname, color): |
|
191 |
|
# pen = pg.mkPen(color=color) |
|
192 |
|
# self.graphWidget.plot(x, y, name=plotname, pen=pen, symbol='+', symbolSize=30, symbolBrush=(color)) |
|
193 |
|
# |
|
194 |
|
#def main(): |
|
195 |
|
# app = QtWidgets.QApplication(sys.argv) |
|
196 |
|
# main = MainWindow() |
|
197 |
|
# main.show() |
|
198 |
|
# sys.exit(app.exec_()) |
|
199 |
|
# |
|
200 |
|
#if __name__ == '__main__': |
|
201 |
|
# main() |
|
202 |
121 |
|
|
203 |
|
|
|
204 |
|
|
|
205 |
|
#import pyqtgraph.exporters |
|
206 |
|
#import numpy as np |
|
207 |
|
# |
|
208 |
|
## define the data |
|
209 |
|
#theTitle = "pyqtgraph plot" |
|
210 |
|
#y = [2,4,6,8,10,12,14,16,18,20] |
|
211 |
|
#y2 = [0,1,2,4,12,14,16,17,14,22] |
|
212 |
|
#x = range(0,10) |
|
213 |
|
# |
|
214 |
|
## create plot |
|
215 |
|
#plt = pg.plot() |
|
216 |
|
#plt.showGrid(x=True,y=True) |
|
217 |
|
#plt.addLegend() |
|
218 |
|
# |
|
219 |
|
## set properties |
|
220 |
|
#plt.setLabel('left', 'Value', units='V') |
|
221 |
|
#plt.setLabel('bottom', 'Time', units='s') |
|
222 |
|
#plt.setXRange(0,10) |
|
223 |
|
#plt.setYRange(0,20) |
|
224 |
|
#plt.setWindowTitle('pyqtgraph plot') |
|
225 |
|
# |
|
226 |
|
## plot |
|
227 |
|
#c1 = plt.plot(x, y, pen='b', symbol='x', symbolPen='b', symbolBrush=0.2, name='red') |
|
228 |
|
#c2 = plt.plot(x, y2, pen='r', symbol='o', symbolPen='r', symbolBrush=0.2, name='blue') |
|
229 |
|
# |
|
230 |
|
### Start Qt event loop. |
|
231 |
|
#if __name__ == '__main__': |
|
232 |
|
# import sys |
|
233 |
|
# if sys.flags.interactive != 1 or not hasattr(pg.QtCore, 'PYQT_VERSION'): |
|
234 |
|
# pg.QtGui.QApplication.exec_() |
|
235 |
|
|
|
236 |
|
|
|
237 |
|
# |
|
238 |
|
## nikdo mi neuvěří, že by tohle postačílo a nebylo by nutné tohlensto furt úpravovat |
|
239 |
|
#def gp_plot(sample_box, space='R', terminal='png', filename=''): |
|
240 |
|
# if not filename: |
|
241 |
|
# filename = 'store/%s_%s_%s'%(sample_box.gm_signature, space, sample_box.nsim) |
|
242 |
|
# if space in ['Rn', 'GK', 'G']: |
|
243 |
|
# gp.c('set autoscale xy') |
|
244 |
|
# gp.c('set size square') |
|
245 |
|
# gp.c('set zeroaxis') |
|
246 |
|
# elif space in ['P', 'U']: |
|
247 |
|
# gp.c('set xrange [0:1]') |
|
248 |
|
# gp.c('set yrange [0:1]') |
|
249 |
|
# gp.c('set size square') |
|
250 |
|
# #gp.c('set autoscale') |
|
251 |
|
# gp.c('unset zeroaxis') |
|
252 |
|
# else: # R teda? |
|
253 |
|
# gp.c('set size noratio') |
|
254 |
|
# gp.c('set autoscale') |
|
255 |
|
# gp.c('unset zeroaxis') |
|
256 |
|
# |
|
257 |
|
# gp.c('set terminal ' + terminal) |
|
258 |
|
# gp.c('set output "%s.%s"'%(filename, terminal)) |
|
259 |
|
# if os.name == 'posix': |
|
260 |
|
# gp.c('set decimalsign locale "POSIX"') |
|
261 |
|
# |
|
262 |
|
# # legenda |
|
263 |
|
# gp.c('unset key') |
|
264 |
|
# |
|
265 |
|
# # se mi zda, že gp bere data v řadcích |
|
266 |
|
# f_name = "%s_failure.dat" % (filename) |
|
267 |
|
# s_name = "%s_success.dat" % (filename) |
|
268 |
|
# gp.s(getattr(sample_box.failure_samples, space).T, f_name) |
|
269 |
|
# gp.s(getattr(sample_box.success_samples, space).T, s_name) |
|
270 |
|
# |
|
271 |
|
# |
|
272 |
|
# # rozkaz, který předaváme gnuplotovi |
|
273 |
|
# gp_plot = 'plot "%s" title "Success points" w p lc rgb "green", "%s" title "Failure points" w p lc rgb "red"' % (s_name, f_name) |
|
274 |
|
# |
|
275 |
|
# # Kružničky chcete? |
|
276 |
|
# # Кружочки ннада? |
|
277 |
|
# if space in ['Rn', 'G']: |
|
278 |
|
# gp.c('set parametric') |
|
279 |
|
# for i in range(5): |
|
280 |
|
# lw = 2 - i*0.3 |
|
281 |
|
# gp_plot += ', cos(t)*%s,sin(t)*%s notitle w l lc rgb "black" lw %s'%(i+1, i+1, lw) |
|
282 |
|
# |
|
283 |
|
# # ne všichni majó definované hranice |
|
284 |
|
# try: |
|
285 |
|
# bounds = sample_box.get_2D_boundary() |
|
286 |
|
# |
|
287 |
|
# for i in range(len(bounds)): |
|
288 |
|
# bound = getattr(bounds[i], space).T |
|
289 |
|
# gp.s(bound, "%s_boundary_%s.dat"%(filename, i+1)) |
|
290 |
|
# gp_plot += ', "%s_boundary_%s.dat" notitle w l lc rgb "blue"'%(filename, i+1) |
|
291 |
|
# except AttributeError: |
|
292 |
|
# pass |
|
293 |
|
# |
|
294 |
|
# # Plot! |
|
295 |
|
# gp.c(gp_plot) |
|
296 |
|
# |
|
297 |
|
# |
|
298 |
|
# |
|
299 |
|
## nikdo mi neuvěří, že by tohle postačílo a nebylo by nutné tohlensto furt úpravovat |
|
300 |
|
#def plot(data2D, terminal='png', filename=''): |
|
301 |
|
# if not filename: |
|
302 |
|
# filename = 'store/plot_%s'%(len(data2D[0])) |
|
303 |
|
# |
|
304 |
|
# gp.c('set terminal ' + terminal) |
|
305 |
|
# gp.c('set output "%s.%s"'%(filename, terminal)) |
|
306 |
|
# if os.name == 'posix': |
|
307 |
|
# gp.c('set decimalsign locale "POSIX"') |
|
308 |
|
# |
|
309 |
|
# |
|
310 |
|
# # se mi zda, že gp bere data v řadcích |
|
311 |
|
# gp.s(data2D, filename+'.dat') |
|
312 |
|
# |
|
313 |
|
# # Plot! |
|
314 |
|
# # rozkaz, který předaváme gnuplotovi |
|
315 |
|
# gp.c('plot "%s.dat" ' % (filename)) |
|