简体   繁体   English

如何仅清除 pyqt5 中的图?

[英]How to clear only plots in pyqt5?

I have created a GUI that shows 6 plots in canvas importing multiple files but I would like to make one button to clear only plots (Data graph) to redraw plot next time.我创建了一个 GUI,显示 canvas 中的 6 个绘图导入多个文件,但我想制作一个按钮以仅清除绘图(数据图)以重绘 plot。 I already made "Clear" Button but it doesn't work properly.我已经制作了“清除”按钮,但它不能正常工作。 How can I do with this?我该怎么办?

***read me: This program has been made only to be asked here. ***读我:这个程序只是为了在这里被问到。 So when you run this code, just click "importing file and plot" button and select any files that you have and the multiple files you choose are used only for making name of the legend in plot and the data for plot is already in my code to generate plot.因此,当您运行此代码时,只需单击“导入文件和绘图”按钮和 select 您拥有的任何文件以及您选择的多个文件仅用于在 plot 中制作图例名称,并且 Z32FA6E1B78A9D40248953 中的数据已经在代码中生成 plot。

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_()

The code编码

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

is incorrect, since fig.axes is a list of axes, and does not have a clear() function.不正确,因为fig.axes是轴列表,并且没有clear() function。

You need to do你需要做

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

@DizietAsahi answer works, you need to update the canvas @DizietAsahi 答案有效,您需要更新 canvas

...
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()                       # +++

...

在此处输入图像描述

在此处输入图像描述

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

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