简体   繁体   English

使用工具栏更新pyqt5 gui中的嵌入式matplotlib图

[英]Update an embedded matplotlib plot in a pyqt5 gui with toolbar

In this post I asked how to embed a matplotlib plot in a pyqt5 gui which is working fine now. 在这篇文章中,我问了如何在pyqt5 gui中嵌入一个matplotlib图,它现在可以正常工作。 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. 每次带有一个新的“ fig”对象,但是什么也没有发生。 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_())

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM