首页 > 解决方案 > 如何只清除pyqt5中的图?

问题描述

我创建了一个 GUI,在画布中显示 6 个绘图,导入多个文件,但我想制作一个按钮来仅清除绘图(数据图)以在下次重绘绘图。我已经制作了“清除”按钮,但它不能正常工作。我该怎么办?

***读我:这个程序只是为了在这里被问到。因此,当您运行此代码时,只需单击“导入文件和绘图”按钮并选择您拥有的任何文件,您选择的多个文件仅用于在绘图中制作图例的名称,并且绘图的数据已经在我的代码中生成情节。

import sys
from PyQt5.QtWidgets import QMessageBox, QWidget, QHBoxLayout, QPushButton, QSpinBox, QLabel, 
QVBoxLayout, QFileDialog, QApplication, QRadioButton
from PyQt5.QtGui import QIcon
import matplotlib.pyplot as plt
import matplotlib.widgets as mwg
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from math import log10
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()
        self.f_range = '2'
        self.l_range = '3'
    def setupUI(self):
        self.setGeometry(200, 50, 1500, 950)
        self.setWindowTitle("Material Properties Viewer v1.0")
        self.setWindowIcon(QIcon('icon.png'))
        self.lable = QLabel("Select the Temperature", self)
        self.spinBox = QSpinBox(self)
        self.spinBox.setValue(40)
        self.spinBox.setSingleStep(10)
        self.spinBox.setMinimum(-30)
        self.spinBox.setMaximum(100)
        self.spinBox.valueChanged.connect(self.spinBoxChanged)
        self.VIS_Button = QPushButton("Importing File and Plot")
        self.CL_Button = QPushButton("Clear All")
        self.Plabel = QLabel("Select the Frequency ", self)
        self.radio1 = QRadioButton('200- 1000 Hz')
        self.radio1.clicked.connect(self.radioButtonClicked)
        self.radio2 = QRadioButton('200- 20000 Hz')
        self.radio2.clicked.connect(self.radioButtonClicked)
        self.radio3 = QRadioButton('10000- 20000 Hz')
        self.radio3.clicked.connect(self.radioButtonClicked)
        self.radio1.setChecked(True)
        self.VIS_Button.clicked.connect(self.VIS_ANALYSIS)
        self.CL_Button.clicked.connect(self.Clear)
        self.fig = plt.Figure()
        self.ax1 = self.fig.add_subplot(231, position=[0.045, 0.55, 0.275, 0.4])  
        self.ax1.set_title('Modulus1')
        self.ax1.set_xlabel('Frequency')
        self.ax1.set_ylabel('Modulus')
        self.ax2 = self.fig.add_subplot(232, position=[0.375, 0.55, 0.275, 0.4]) 
        self.ax2.set_title('Modulus2')
        self.ax2.set_xlabel('Frequency')
        self.ax2.set_ylabel('Modulus')
        self.ax3 = self.fig.add_subplot(233, position=[0.7, 0.55, 0.275, 0.4]) 
        self.ax3.set_title('Modulus3')
        self.ax3.set_xlabel('Frequency')
        self.ax3.set_ylabel('Modulus')
        self.ax4 = self.fig.add_subplot(234, position=[0.045, 0.06, 0.275, 0.4])
        self.ax4.set_title('Modulus4')
        self.ax4.set_xlabel('Frequency')
        self.ax4.set_ylabel('Modulus')
        self.ax5 = self.fig.add_subplot(235, position=[0.375, 0.06, 0.275, 0.4])
        self.ax5.set_title('Modulus5')
        self.ax5.set_xlabel('Frequency')
        self.ax5.set_ylabel('Modulus')
        self.ax6 = self.fig.add_subplot(236, position=[0.7, 0.06, 0.275, 0.4])
        self.ax6.set_title('Modulus6')
        self.ax6.set_xlabel('Frequency')
        self.ax6.set_ylabel('Modulus')
        self.ax1.grid(True)
        self.ax2.grid(True)
        self.ax3.grid(True)
        self.ax4.grid(True)
        self.ax5.grid(True)
        self.ax6.grid(True)
        self.canvas = FigureCanvas(self.fig)
        leftLayout = QVBoxLayout()
        leftLayout.addWidget(self.canvas)
        rightLayout = QVBoxLayout()
        rightLayout.addWidget(self.lable)
        rightLayout.addWidget(self.spinBox)
        rightLayout.addWidget(self.Plabel)
        rightLayout.addWidget(self.radio1)
        rightLayout.addWidget(self.radio2)
        rightLayout.addWidget(self.radio3)
        rightLayout.addWidget(self.VIS_Button)
        rightLayout.addWidget(self.CL_Button)
        rightLayout.addStretch(1)
        layout = QHBoxLayout()
        layout.addLayout(leftLayout)
        layout.addLayout(rightLayout)
        layout.setStretchFactor(leftLayout, 1)
        layout.setStretchFactor(rightLayout, 0)
        self.setLayout(layout)
    def Clear(self):
        self.fig.axes.clear()
    def spinBoxChanged(self):
        self.spinBox.value()
    def radioButtonClicked(self):
        if self.radio1.isChecked():
            self.f_range = "2"  # 200 - 1000
            self.l_range = "3"
        elif self.radio2.isChecked():
            self.f_range = "2"  # 200 - 20000
            self.l_range = "4.5"
        elif self.radio3.isChecked():
            self.f_range = "4"
            self.l_range = "4.5"
    def VIS_ANALYSIS(self):
        fname = QFileDialog.getOpenFileNames(self)
        filename = list(fname[0])
        for i in range(len(filename)):
            lable_name = filename[i].split('/')[-1][:-4]
            self.ax1.plot([0,1,2,3], [0,1,2,3], label=lable_name)
            self.ax1.legend(loc="upper right")
            self.ax2.plot([0,1,2,3], [0,1,2,3], label=lable_name)
            self.ax2.legend(loc="upper right")
            self.ax3.plot([0,1,2,3], [0,1,2,3], label=lable_name)
            self.ax3.legend(loc="upper right")
            self.ax4.plot([0,1,2,3], [0,1,2,3], label=lable_name)
            self.ax4.legend(loc="upper right")
            self.ax5.plot([0,1,2,3], [0,1,2,3], label=lable_name)
            self.ax5.legend(loc="upper right")
            self.ax6.plot([0,1,2,3], [0,1,2,3], label=lable_name)
            self.ax6.legend(loc="upper right")
            self.canvas.draw()
    def PYN_ANALYSIS(self):
        QMessageBox.about(self, "Warning", "It is not available yet..!")
    def closeEvent(self, QCloseEvent):
        ans = QMessageBox.question(self, 'Confirm Close', 'Are you sure you want to close?',
                                   QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if ans == QMessageBox.Yes:
            QCloseEvent.accept()
        else:
            QCloseEvent.ignore()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyWidget()
    window.show()
    app.exec_()

标签: pythonpython-3.xmatplotlibpyqtpyqt5

解决方案


编码

def Clear(self):
    self.fig.axes.clear()

不正确,因为fig.axes是轴列表,并且没有clear()功能。

你需要做

def Clear(self):
    for ax in self.fig.axes:
        ax.clear()

推荐阅读