Python Forum
[PyQt] Refresh x-labels in matplotlib animation widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Refresh x-labels in matplotlib animation widget

I'm working on my first pyQt gui. I'm using QtDesigner.

I've added four widgets on which I draw live sensor data. I'm Using the matplotlib.animation class and everything is working quite well so far. I get the four individual graphs on each widget.

My problem ist, that the x and y axis autoscale according to the incoming data, but the tick labels are static.
From my research so far I know, that this is a result of the blit=True argument in the FuncAnimation() routine. If blitting is enabled the axis and so on are not plotted again.

I've tried to disable the blitting but by doing so there is no data plotted at all. The only thing that worked so far was to add a MplWidget.self.draw() to the update function. This leads to the fact, that the whole plot gets plotted again and again which ends up in performance problems. So not a good solution.

Is there any way to automatically refresh the x-tick labels in the animation and not losing too much performance?

Any help is appriciated

Thanks a lot
Without seeing the code, nobody will be able to help.
Hi and sorry for my late response,

I thought thats its a general problem so I kept out the code. I chopped down the code to the essentials and will add it here.
The code is working so I dont post the imports and so on.

For testing purposes the live data is simulated by a .csv-file which is read in the initialisation. The test values are in range of 100 to 200. My problem is, that the labels of the x-ticks and the ticks itselfs are not refreshed according to the plotted data. The horizontal size of the graph is changing as further data is plotted, so in the background the setting of the x limits seems to work.

Any ideas? Thanks a lot

class Live_Window(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.actRow = 10                  
        self.axis = self.figure.subplots()   
        self.axis.tick_params(axis='x', colors='white')
        self.axis.tick_params(axis='y', colors='white')

class MainWidget(QWidget, XYZ.Ui_Dialog):
    def __init__(self):
        self.counter = 1
        self.newData = pd.read_csv('Data.csv', sep=';')
        self.Time = self.newData.iloc[:,0]-self.newData.iloc[0,0]
        self.newData.iloc[:,0] = self.Time
    def init_Widget(self):
        self.matplotlib_widget1 = Live_Window()
        self.layoutvertical1 = QVBoxLayout(self.plotWidget)
        #create empty lists
        self.x = list(range(0,0))
        self.y = list(range(0,0))     
        self.line1, =  self.matplotlib_widget1.axis.plot(self.x, self.y, lw=1, color = 'chocolate')
    def update_line(self, i):
        ## append new data to data line
        self.y.append(self.newData.iloc[self.counter, self.matplotlib_widget1.actRow])
        self.x.append(self.newData.iloc[self.counter, 0])
        self.counter += 1
        return [self.line1]
    ## triggered by pushbutton
    def startMeasurement(self):
       self.ani1 = FuncAnimation( self.matplotlib_widget1.figure, self.update_line, 
                                blit=True, interval=40)
if __name__ == '__main__':
    APP = QApplication(sys.argv)
    GUI = MainWidget()
This is only part of the code and is useless.

You don't have any imports (PyQt5, matplotlib, pandas ...)

What is XYZ.Ui_Dialog?
(Apr-21-2021, 03:26 PM)Axel_Erfurt Wrote: This is only part of the code and is useless.

You don't have any imports (PyQt5, matplotlib, pandas ...)

What is XYZ.Ui_Dialog?

I've written, that i left out the imports because it's not an import problem as the code is running correctly.
Here are the imports

XYZ ist the converted .ui file which I created in the QtDesigner. I created a empty widget in QtDesigner called "plotWidget" used in the init_Widget function.

from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout,QLabel, QInputDialog, QLineEdit, QFileDialog
from PyQt5 import QtCore, QtGui

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvas
import matplotlib as mpl
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Cursor, MultiCursor

import numpy as np
import pandas as pd
import sys

import XYZ
Any hints? Or is some information missing?

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - update/refresh treeview snakes 4 11,770 May-13-2021, 07:10 AM
Last Post: snakes
  Refresh image in label after every 1s using simple function jenkins43 1 4,738 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  Unable to update or refresh label text in tkinter jenkins43 3 5,085 Jul-24-2019, 02:09 PM
Last Post: Friend
  Tkinter refresh eduardoforo 4 11,060 Nov-14-2018, 09:35 PM
Last Post: woooee
  [Tkinter] Label doesn't refresh jollydragon 7 5,770 Jul-13-2018, 05:55 AM
Last Post: jollydragon
  [PyQt] enviremont refresh duende 2 2,744 May-13-2018, 09:49 AM
Last Post: duende
  pyqt main window refresh poblems duende 0 4,525 Apr-13-2018, 05:05 PM
Last Post: duende
  Refresh test in urwid.Text Stensborg 1 3,772 Mar-05-2018, 11:23 AM
Last Post: Stensborg
  [Tkinter] problem with refresh UI in tkinter app Philbot 5 9,174 Feb-06-2018, 01:10 PM
Last Post: Philbot
  How Can I Do Refresh Window? satrancali 0 6,254 Feb-03-2018, 07:50 AM
Last Post: satrancali

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020