3

I am trying to plot some receiving data from a sensor using matplotlib.animation.FuncAnimation(). In addition, I would like to use blit=True because as far as I know it makes the program more efficient (taking advantage of the already plotted values and only refreshing the new ones).

However, when I use blit=True and autoscale_view() and relim() the x_axis values are not autoscaling and stay all time the same. So my question is if this is possible or blit option can only be used for "loop" o "repeating" graphs in which the x_axis is always the same (on Internet I only found examples likes these: sine graph, spirals...). Additionally, when using blit my graph seems to be blind or cut-out at some limit points (bottom and right side of the graph) so all points cannot be seen.

This is a example of my code (plotting random values instead of reading from sensor):

import sys, os, random, matplotlib
import numpy as np
from datetime import datetime
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QSizePolicy, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


class MyMplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.ax = fig.add_subplot(1,1,1)
        FigureCanvas.__init__(self, fig)
        FigureCanvas.setSizePolicy(self,
                                   QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")
        self.main_widget = QtWidgets.QWidget(self)
        self.xdata = []
        self.ydata = []
        vbox = QtWidgets.QVBoxLayout(self.main_widget)
        self.canvas =  MyMplCanvas( self.main_widget,width=6, height=6, dpi=100) ###attention###
        vbox.addWidget(self.canvas)
        self.setLayout(vbox)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

        self.line, = self.canvas.ax.plot_date(self.xdata, self.ydata,'-')
        self.canvas.ax.tick_params(axis='x', rotation=50)
        self.ani = FuncAnimation(self.canvas.figure, self.update_line, frames=self.gen_function_time, blit=True ,interval=1000)

    def gen_function_time(self):
        while True: 
            self.now = datetime.now()
            yield self.now

    def update_line(self, frame):
        self.xdata.append(frame)
        self.ydata.append(1+np.random.randint(-3,3))
        self.line.set_data(self.xdata, self.ydata)
        self.canvas.ax.relim()
        self.canvas.ax.autoscale_view()
        return self.line,


if __name__ == "__main__":
    App = QApplication(sys.argv)
    aw = ApplicationWindow()
    aw.show()
    App.exit()
    sys.exit(App.exec_())
Sergio
  • 68
  • 5

0 Answers0