【问题标题】:I am getting unwanted loading of previous plot axis points to the next plot in PyQt5 matplotlib我在 PyQt5 matplotlib 中将先前的绘图轴点不必要地加载到下一个绘图
【发布时间】:2021-07-18 20:55:59
【问题描述】:

我正在尝试在选择新文件后更新绘图,但是生成的新绘图在 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_()

【问题讨论】:

    标签: python-3.x matplotlib plot pyqt5


    【解决方案1】:

    每次按下按钮时,self.ax = self.canvas.figure.subplots() 将创建一组新轴并将其添加到先前创建的子图网格中的 (0,0) 位置。由于所有子图都放置在网格中的相同位置,它们都重叠。为了解决这个问题,您可以在MainWindow.__init__ 中创建一组轴,并在MainWidon.get_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.ax.bar(...) 之后添加self.canvas.draw_idle()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多