3

In this post I asked how to embed a matplotlib plot in a pyqt5 gui which is working fine now. Now I have a further problem that I couldn't solve until now: how can I update the figure?

I tried to update the canvas by calling

self.plotWidget = FigureCanvas(fig)

every time with a new "fig" object but nothing happened. So I tried to add it to the layout

self.lay.addWidget(self.plotWidget)

but this adds another subplot to the widget. I guess that I have to clear the previous plot before updating but I don't know how to do it.

What is the correct strategy to replot using the update method?

Thanks!

# -*- coding: utf-8 -*-

import sys
import numpy as np   

from PyQt5 import QtCore, QtWidgets, uic

import matplotlib
matplotlib.use('QT5Agg')

import matplotlib.pylab as plt

from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5
from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

class MyWindow(QMainWindow):

    def __init__(self):

        super(MyWindow, self).__init__()
        uic.loadUi('test.ui', self) 

        # test data
        data = np.array([0.7,0.7,0.7,0.8,0.9,0.9,1.5,1.5,1.5,1.5])        
        fig, ax1 = plt.subplots()
        bins = np.arange(0.6, 1.62, 0.02)
        n1, bins1, patches1 = ax1.hist(data, bins, alpha=0.6, density=False, cumulative=False)

        # plot
        self.plotWidget = FigureCanvas(fig)
        self.lay = QtWidgets.QVBoxLayout(self.content_plot)  
        self.lay.setContentsMargins(0, 0, 0, 0)      
        self.lay.addWidget(self.plotWidget)

        # add toolbar
        self.addToolBar(QtCore.Qt.BottomToolBarArea, NavigationToolbar(self.plotWidget, self))        

        # button event
        self.pushButton.clicked.connect(self.update)

        # show window
        self.show() 

        #########################################

    def update(self):

        data = np.array([0.1,0.1,0.1,0.1,0.3,0.3,1.7,1.7,1.7,1.7])        
        fig, ax1 = plt.subplots()
        bins = np.arange(0.6, 1.62, 0.02)
        n1, bins1, patches1 = ax1.hist(data, bins, alpha=0.6, density=False, cumulative=False)

        # show plot in canvas  
        self.plotWidget = FigureCanvas(fig)
        self.toolbarWidget = NavigationToolbar(self.plotWidget, self)        


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

1 Answer 1

7

You should not create a new figure, instead, reuse the axes that you already have. That means that you have to keep a reference to that object first.

I cannot test the code right now, but you should do something like

def __init__(self):
    (...)
    self.fig, self.ax = plt.subplots()
    (...)

def update(self):
    data = (...)
    self.ax.cla()  # clear the axes content
    self.ax.hist(...)
    self.fig.canvas.draw_idle()  # actually draw the new content
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That is easy... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.