如何删除嵌入PyQt5的Matplotlib图形上的旧图?

时间:2018-08-11 00:46:16

标签: matplotlib

我正在使用pyqt5中嵌入的matplotlib图形来绘制一帧,并从行编辑条目中获取高度和宽度。效果很好,但是当我更改行编辑中的值并单击按钮时,它将绘制另一帧在上一个。我尝试了plt.gcf()。clear()和ax.clear,但它们没有用。

class Window(QMainWindow):
def __init__(self):
    super().__init__()
    self.setWindowTitle("Draw Frame")
    self.setGeometry(100,100,680, 450)

    # Creation of figure and canvas
    self.figure = plt.figure()
    self.canvas = FigureCanvas(self.figure)
    self.ax = self.figure.add_subplot(111)
    self.ax.axis("off")
    #self.toolbar = NavigationToolbar(self.canvas, self)
    self.plot_widget = QWidget(self)
    self.plot_widget.setGeometry(250, 10, 400, 400)
    plot_box = QVBoxLayout()
    plot_box.addWidget(self.canvas)
    self.plot_widget.setLayout(plot_box)

    self.label1=QLabel("Frame height",self)
    self.label1.move(10,30)
    self.label2 = QLabel("Frame height", self)
    self.label2.move(10, 70)

    self.lineEdit1=QLineEdit(self)
    self.lineEdit1.move(100,30)
    self.lineEdit1.setText("10")
    self.lineEdit2 = QLineEdit(self)
    self.lineEdit2.move(100, 70)
    self.lineEdit2.setText("20")

    self.button = QPushButton('Draw Frame', self)
    self.button.clicked.connect(self.plot)
    self.button.move(70, 350)

    self.show()

def plot(self):
    if len(self.lineEdit1.text())!=0:
        self.h=int(self.lineEdit1.text())
    else:
        self.h=0
    if len(self.lineEdit2.text()) != 0:
        self.w=int(self.lineEdit2.text())
    else:
        self.w=0

    x = [0, 0, self.w, self.w]
    y = [0, self.h, self.h, 0]
    self.ax.plot(x, y)
    self.canvas.draw()

Picture of the app

2 个答案:

答案 0 :(得分:0)

如上面的注释中所述,您每次单击按钮时都需要调用self.ax.clear()。否则,您将在同一图上重画。不知道为什么您的图形不会在clear()上重置,但是这是我运行的代码,效果很好。让我知道它是否对您有用:

from PyQt5.QtWidgets import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import sys

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Draw Frame")
        self.setGeometry(100,100,680, 450)

        # Creation of figure and canvas
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.ax = self.figure.add_subplot(111)
        self.ax.axis("off")
        #self.toolbar = NavigationToolbar(self.canvas, self)
        self.plot_widget = QWidget(self)
        self.plot_widget.setGeometry(250, 10, 400, 400)
        plot_box = QVBoxLayout()
        plot_box.addWidget(self.canvas)
        self.plot_widget.setLayout(plot_box)

        self.label1=QLabel("Frame height",self)
        self.label1.move(10,30)
        self.label2 = QLabel("Frame height", self)
        self.label2.move(10, 70)

        self.lineEdit1=QLineEdit(self)
        self.lineEdit1.move(100,30)
        self.lineEdit1.setText("10")
        self.lineEdit2 = QLineEdit(self)
        self.lineEdit2.move(100, 70)
        self.lineEdit2.setText("20")

        self.button = QPushButton('Draw Frame', self)
        self.button.clicked.connect(self.plot)
        self.button.move(70, 350)

        self.show()

    def plot(self):
        self.ax.clear()
        if len(self.lineEdit1.text())!=0:
            self.h=int(self.lineEdit1.text())
        else:
            self.h=0
        if len(self.lineEdit2.text()) != 0:
            self.w=int(self.lineEdit2.text())
        else:
            self.w=0

        x = [0, 0, self.w, self.w]
        y = [0, self.h, self.h, 0]
        self.ax.plot(x, y)
        self.canvas.draw()

if __name__=='__main__':
    app = QApplication(sys.argv)
    w = Window()
    app.exec_()

答案 1 :(得分:0)

代替清除轴,您可以更新使用新坐标绘制的线。

from PyQt5.QtWidgets import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import sys

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Draw Frame")
        self.setGeometry(100,100,680, 450)

        # Creation of figure and canvas
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.ax = self.figure.add_subplot(111)
        self.ax.axis("off")
        self.line, = self.ax.plot([])
        self.ax.axis([0,100,0,100])
        #self.toolbar = NavigationToolbar(self.canvas, self)
        self.plot_widget = QWidget(self)
        self.plot_widget.setGeometry(250, 10, 400, 400)
        plot_box = QVBoxLayout()
        plot_box.addWidget(self.canvas)
        self.plot_widget.setLayout(plot_box)

        self.label1=QLabel("Frame height",self)
        self.label1.move(10,30)
        self.label2 = QLabel("Frame height", self)
        self.label2.move(10, 70)

        self.lineEdit1=QLineEdit(self)
        self.lineEdit1.move(100,30)
        self.lineEdit1.setText("10")
        self.lineEdit2 = QLineEdit(self)
        self.lineEdit2.move(100, 70)
        self.lineEdit2.setText("20")

        self.button = QPushButton('Draw Frame', self)
        self.button.clicked.connect(self.plot)
        self.button.move(70, 350)

        self.show()

    def plot(self):
        if len(self.lineEdit1.text())!=0:
            self.h=int(self.lineEdit1.text())
        else:
            self.h=0
        if len(self.lineEdit2.text()) != 0:
            self.w=int(self.lineEdit2.text())
        else:
            self.w=0

        x = [0, 0, self.w, self.w]
        y = [0, self.h, self.h, 0]
        self.line.set_data(x,y)
        self.canvas.draw()

if __name__=='__main__':
    app = QApplication(sys.argv)
    w = Window()
    app.exec_()