This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
43 lines (36 loc) · 1.58 KB
/
plot.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
import pyqtgraph as pg
class graph():
def __init__(self,pens = [{"color":(255,0,0),"name":"NaN"}] , moving=False , n_data=50, x_name="" , y_name="" , title=""):
#When showing a moving graph n_data decides the no. of elements shown
self.moving = moving
self.n_data = n_data
# Creating Graph Widget
self.graphWidget = pg.PlotWidget()
self.graphWidget.setBackground((0,0,0))
# Making plots
self.plots=[]
for pen in pens :
self.plots.append({
"x" : [],
"y" : [],
"pen" : pg.mkPen(color=pen["color"],width=2),
"name" : pen["name"]
})
# Add Title
self.graphWidget.setTitle(title, color="w", size="5pt")
# Add Axis Labels
styles = {"color": "#FFFFFF", "font-size": "15px"}
self.graphWidget.setLabel("left", y_name, **styles)
self.graphWidget.setLabel("bottom", x_name, **styles)
def update(self,x,y):
for i,plot in enumerate(self.plots) :
if y[i]:
plot["y"].append(y[i])
plot["x"].append(x)
if len(plot["x"]) == 1 :
self.graphWidget.addLegend()
plot["data_line"] = self.graphWidget.plot(plot["x"],plot["y"], pen=plot["pen"],name=plot["name"])
if self.moving and len(plot["x"]) > self.n_data:
plot["y"] = plot["y"][1:] # Remove the first
plot["x"] = plot["x"][1:] # Remove the first
plot["data_line"].setData(plot["x"], plot["y"]) # Update the data.