如何仅清除qyqt5中的图?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何仅清除qyqt5中的图?相关的知识,希望对你有一定的参考价值。

我创建了一个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_()
答案

代码

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

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

您需要做

def Clear(self):
    for ax in self.fig.axes:
        ax.clear()
另一答案

@@ zizietAsahi答案有效,您需要更新画布

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

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

    self.ax1.set_title('Modulus1')
    self.ax1.set_xlabel('Frequency')
    self.ax1.set_ylabel('Modulus')
    self.ax2.set_title('Modulus2')
    self.ax2.set_xlabel('Frequency')
    self.ax2.set_ylabel('Modulus')
    self.ax3.set_title('Modulus3')
    self.ax3.set_xlabel('Frequency')
    self.ax3.set_ylabel('Modulus')
    self.ax4.set_title('Modulus4')
    self.ax4.set_xlabel('Frequency')
    self.ax4.set_ylabel('Modulus')
    self.ax5.set_title('Modulus5')
    self.ax5.set_xlabel('Frequency')
    self.ax5.set_ylabel('Modulus')
    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)            

    # refresh canvas            
    self.canvas.draw()                       # +++

...

enter image description here

enter image description here

以上是关于如何仅清除qyqt5中的图?的主要内容,如果未能解决你的问题,请参考以下文章

如何清除片段中的参数?

如何从片段外部清除/重置地图?

如何使用 jquery 在 datalist 中仅采用最后五个选项并清除 datalist 中的所有其他选项?

滑动 ViewPager 时如何清除片段相关状态

片段中的Tablayout + viewpager,需要清除viewpager中的edittext数据

清除 ASP.net 中的按钮名称参数