【问题标题】:Add and Remove plots to a matplotlib figure向 matplotlib 图添加和删除图
【发布时间】:2019-08-03 16:34:56
【问题描述】:

我有一个使用 PyQt5 开发的界面,带有 4 个按钮(2 个添加按钮和 2 个删除按钮)。我可以绘制第一个数据集,但是当我绘制第二个数据集时,第一个数据集被删除。这个想法是有可能通过单击按钮来添加或删除数据集。这里是初始代码:

class Mainwindow(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(Mainwindow, self).__init__(parent)
        self.setupUi(self)        
        self.btn_add_1.clicked.connect(self.add_1)
        self.btn_add_2.clicked.connect(self.add_2)
        self.btn_remove_1.clicked.connect(self.remove_1)
        self.btn_remove_2.clicked.connect(self.remove_2)

    def add_1(self):
        self.x = [1,2,3,4,5]
        self.y = [1,2,3,4,5]
        self.add_selected = 'add_1'
        self.Graphique = Graphique(self.add_selected, self.x, self.y, self)
        self.gridLayout.addWidget(self.Graphique, 0, 0, 1, 1)     

    def add_2(self):
        self.x = [1,2,3,4,5]
        self.y = [1,4,6,8,10]
        self.add_selected = 'add_2'
        self.Graphique = Graphique(self.add_selected, self.x, self.y, self)
        self.gridLayout.addWidget(self.Graphique, 0, 0, 1, 1)

    def remove_1(self):
        pass

    def remove_2(self):
        pass

class Graphique(FigureCanvas):

    def __init__(self, add_selected, x, y, parent =None):

        self.x = x
        self.y = y
        self.add_selected = add_selected

        self.fig = Figure()        
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)   
        self.Populate()

    def Populate(self):
        self.axes = self.figure.add_subplot(1,1,1)           
        lines = self.axes.plot(self.x, self.y,'o-')

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    prog = Mainwindow()   
    prog.showMaximized()
    sys.exit(app.exec_())

【问题讨论】:

  • 目前每个数据集都有自己的图。相反,您可能希望创建一个图形并只更改数据,而不是完整的图形。

标签: python-3.x matplotlib pyqt5


【解决方案1】:

试试看:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure

import random


class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.xs   = list(line.get_xdata())
        self.ys   = list(line.get_ydata())

        self.btnAdd = QPushButton("add datasets just by clicking buttons")
        self.btnAdd.clicked.connect(self.btnClick)

    def btnClick(self):
#        self.x = [1,2,3,4,5]
#        self.y = [1,2,3,4,5]  
        self.x = random.sample(range(100), 5) 
        self.y = random.sample(range(100), 5)        

        self.xs.append(self.x)
        self.ys.append(self.y)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw()
#        print(f" \n xs={self.xs}, \n ys={self.ys}")



class Mainwindow(QMainWindow):                      #, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Mainwindow, self).__init__(parent)

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)

        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        self.ax.set_title('Click to add a dataset')
        self.line, = self.ax.plot([], [], 'o-')                              
        self.canvas = FigureCanvas(self.fig)

        self.gridLayout = QGridLayout(centralWidget)
        self.gridLayout.addWidget(self.canvas)        

        self.ax.set_ylim([0, 100])
        self.ax.set_xlim([0, 100])

        self.LineBuilder = LineBuilder(self.line)
        self.gridLayout.addWidget(self.LineBuilder.btnAdd, 2, 0, 1, 1)



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    prog = Mainwindow()   
    prog.showMaximized()
    sys.exit(app.exec_())

【讨论】:

  • 感谢您的回答,它几乎可以解决我的问题。我需要在同一个图中绘制散点图和常规图(可以通过单击另一个按钮删除常规图)。我通过添加以下代码尝试了您的方法:self.heat = self.ax.scatter([], [], marker='o')self.heat.set_data(self.xs, self.ys) 但我收到此错误AttributeError: 'PathCollection' object has no attribute 'set_data'
猜你喜欢
  • 1970-01-01
  • 2012-03-04
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 2012-03-06
相关资源
最近更新 更多