Python 3.x 我在PyQt5 matplotlib中将上一个绘图轴点的不必要加载到下一个绘图

Python 3.x 我在PyQt5 matplotlib中将上一个绘图轴点的不必要加载到下一个绘图,python-3.x,matplotlib,plot,pyqt5,Python 3.x,Matplotlib,Plot,Pyqt5,我正在尝试在选择新文件后更新绘图,但生成的新绘图在X轴和Y轴上都有上一个绘图的点,我不想删除这些上一个点,请任何人解释为什么会发生这种情况以及如何消除这种情况。此处显示图像,上一个绘图为 在此之后,我选择第二个不同数据的文件来打印它,下一个打印就是这个图像 我试图构建的代码是 import matplotlib.pyplot as plt from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanv

我正在尝试在选择新文件后更新绘图,但生成的新绘图在X轴和Y轴上都有上一个绘图的点,我不想删除这些上一个点,请任何人解释为什么会发生这种情况以及如何消除这种情况。此处显示图像,上一个绘图为

在此之后,我选择第二个不同数据的文件来打印它,下一个打印就是这个图像

我试图构建的代码是

import matplotlib.pyplot as plt

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from PyQt5.QtWidgets import (QApplication, QWidget, QFileDialog, QPushButton, QLabel, QGridLayout, QVBoxLayout, QLineEdit)

from Bio import SeqIO
from collections import Counter
from Bio.SeqUtils import molecular_weight
from Bio.SeqUtils import GC

class MainWindow(QWidget):
    
    def __init__(self):
        super().__init__()
       
        self.setWindowTitle("DNA Sequence Analysis - Prashik Lokhande")
        self.setLayout(QVBoxLayout())
        
        my_label = QLabel("DNA Sequence Analysis from the FASTA Database, (FASTA databse can be found on NCBI website). Build by Prashik Lokhande")
        self.layout().addWidget(my_label)
  
        self.visualize()
        self.show()
        
        
    def visualize(self):
        container = QWidget()
        container.setLayout(QGridLayout())
        
        label_1 = QLabel("PLease Select FASTA file")
        
        button_1 = QPushButton("Select file", clicked = lambda: self.get_plot())
        gc_count_label = QLabel("GC Count = ")
        self.gc_count_field = QLabel("0")
        
        self.canvas = FigureCanvas(plt.Figure(figsize=(10, 4)))
        
        container.layout().addWidget(label_1, 0,0)
        container.layout().addWidget(button_1, 1,0)
        container.layout().addWidget(gc_count_label, 2, 1)
        container.layout().addWidget(self.gc_count_field, 3, 1)
        container.layout().addWidget(self.canvas, 2, 0, 3, 1)
        
        self.layout().addWidget(container)
        
    
    def get_plot(self):
        filepath, _ = QFileDialog.getOpenFileName(self, 'select FASTA file')
        record = SeqIO.read(filepath,"fasta")
        
        dna = record.seq
        mrna = dna.transcribe()
        protein = mrna.translate()
        
        self.mol_weight = molecular_weight(dna)
        
        gc = GC(dna)
        self.gc_count_field.setText(str(gc))
        
        pr_freq = Counter(protein)
        
        self.ax = self.canvas.figure.subplots()
        
        self.ax.bar(pr_freq.keys(), pr_freq.values())
        self.ax.set_title("Amino Acid Contents in the sequence (X-axis Amino acids, Y-axis frequency)")
        
        

app = QApplication([])

mw = MainWindow()

app.exec_()

每次按下按钮,
self.ax=self.canvas.figure.subplot()
将创建一组新的轴,并将其添加到先前创建的子地块网格中的(0,0)位置。由于所有子地块都放置在网格中的同一位置,因此它们都重叠。要解决这个问题,您只需在
main窗口中创建一组轴。然后在
MainWidon.get\u plot
中重用这个轴

class MainWindow(QWidget):
    def __init__(self):
        ....
        self.ax = self.canvas.figure.subplots()

    def get_plot(self):
        ....

        # clear previous plot
        self.ax.clear()
        self.ax.bar(pr_freq.keys(), pr_freq.values())

        ....

是的,这很有效,谢谢你,但我遇到了一个问题,绘图没有更新,要更新它,我需要使用鼠标光标拉伸或收缩窗口,因此你能建议如何处理它吗..你可以尝试在
self.canvas.draw_idle()
之后添加
self.ax.bar(…)
。是的,这很有效,谢谢@Heike