SoFunction
Updated on 2024-11-19

Dynamically updating graphs based on real-time data using matplotlib

I'll cut to the chase and get right to the code!

from time import sleep
from threading importThread
import numpy as np
import  as plt
from  importButton
fig, ax = ()
# Setting the graphic display position
plt.subplots_adjust(bottom=0.2)
# Experimental data
range_start, range_end, range_step =0,1,0.005
t = (range_start, range_end, range_step)
s = (4**t)
l,= (t, s, lw=2)
# Custom class to encapsulate the two button click event handlers
classButtonHandler:
def __init__(self):
 =True
self.range_s, self.range_e, self.range_step =0,1,0.005
# Threaded functions to update data and redraw graphs
def threadStart(self):
while :
sleep(0.02)
self.range_s += self.range_step
self.range_e += self.range_step
t = (self.range_s, self.range_e, self.range_step)
ydata = (4**t)
#Update data
l.set_xdata(t-t[0])
l.set_ydata(ydata)
#Redraw the graph
()
defStart(self, event):
 =True
#Create and start a new thread
t =Thread(target=)
()
defStop(self, event):
 =False
callback =ButtonHandler()
# Create the button and set the click event handler function
axprev = ([0.81,0.05,0.1,0.075])
bprev =Button(axprev,'Stop')
bprev.on_clicked()
axnext = ([0.7,0.05,0.1,0.075])
bnext =Button(axnext,'Start')
bnext.on_clicked()
()

II. Operational results

Above this use of matplotlib to realize the dynamic update of the graph according to real-time data is all that I have shared with you, I hope to be able to give you a reference, and I hope that you will support me more.