在 matplotlib、gtk3、python 中绘制来自传感器的实时数据

时间:2021-01-24 16:44:07

标签: python-3.x matplotlib gtk3

我正在尝试绘制一些来自传感器的数据(不在本文讨论范围内)。

我在空地中创建了一个窗口,背景上的所有内容都可以正常工作。

数据每秒来2次;可能会有延迟。所以要调用 update_graph(x,y,z)

一旦被调用,我只想将这些数据添加到带有当前时间戳的绘图中。 我想限制绘图上的最大数据(参见例如 self.x=self.x[-50:])。

问题是轴根本没有变化,我不知道为什么会这样。

你能帮我吗?

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

0 个答案:

没有答案