I'm trying to plot some data that are coming from sensor (not in scope of this post).
I have a window created in glade and everything on the background just works.
The data are coming 2 times per second; there could a delay. So the update_graph(x,y,z)
is to to be called.
Once it's called I just want to add those data to the plot with current timestamp.
I'd like to limit maximum data that are on the plot (see for e.g. self.x=self.x[-50:]
).
The thing is the axis are not changing at all and I don't know why is that.
Could you please help me out?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
class TestPlot(Gtk.Box):
def __init__(self):
super().__init__()
self.background = None
self.x = [time.time()]
self.y = [0]
self.z = [0]
self.w = [0]
self.fig = Figure()
self.canvas = FigureCanvas(self.fig)
self.ax = self.fig.add_subplot(111, facecolor="#192841")
self.ax.set_title('My data')
self.ax.set_ylabel('data')
self.ax.set_xlabel('time[s]')
# Creating Scrolled window to be able to draw
self.scrolled_window = Gtk.ScrolledWindow()
self.scrolled_window.set_hexpand(True)
self.scrolled_window.set_vexpand(True)
self.scrolled_window.add_with_viewport(self.canvas)
self.red_line, = self.ax.plot(self.x, self.y, "-", color="#BB4430", animated=True)
self.green_line, = self.ax.plot(self.x, self.z, "-", color="#16C172", animated=True)
self.yellow_line,= self.ax.plot(self.x, self.w, "-", color="#F3DFA2", animated=True)
# Graph variable
self.last_ymin = 0;
self.last_ymax = 0;
self.background = self.fig.canvas.copy_from_bbox(self.ax.get_figure().bbox)
self.canvas.mpl_connect("draw_event", self.on_draw)
self.add(self.scrolled_window)
def update_graph(self, x, y, z):
self.x.append(time.time())
ymin = np.min([x,y,z])
ymax = np.max([x,y,z])
if ymin < self.last_ymin:
self.last_ymin = ymin;
if ymax > self.last_ymax:
self.last_ymax = ymax
self.y.append(x)
self.z.append(y)
self.w.append(z)
self.x = self.x[-50:]
self.y = self.y[-50:]
self.z = self.z[-50:]
self.w = self.w[-50:]
self.red_line.set_xdata(self.x)
self.red_line.set_ydata(self.y)
self.green_line.set_xdata(self.x)
self.green_line.set_ydata(self.z)
self.yellow_line.set_xdata(self.x)
self.yellow_line.set_ydata(self.w)
self.fig.canvas.restore_region(self.background)
self.ax.set_xlim(np.min(self.x), np.max(self.x))
self.ax.set_ylim(self.last_ymin, self.last_ymax)
self.ax.draw_artist(self.red_line)
self.ax.draw_artist(self.green_line)
self.ax.draw_artist(self.yellow_line)
self.fig.canvas.blit(self.ax.clipbox)
return True
def save_bg(self):
self.background = self.fig.canvas.copy_from_bbox(self.ax.get_figure().bbox)
def on_draw(self, *args):
self.save_bg()
return False
question from:
https://stackoverflow.com/questions/65873257/plotting-realtime-data-coming-from-sensor-in-matplotlib-gtk3-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…