简体   繁体   English

在Matplotlib图上独立添加/删除图

[英]Add/Delete plots independently on a Matplotlib figure

I want to generate a scatterplot (up to half a million points) and on top of that, add different statistics (eg Q1, median, Q3). 我想生成一个散点图(最多500万点),并在此之上添加不同的统计信息(例如Q1,中位数,Q3)。 The idea is to add/delete those statistics without replotting the scatterplot in order to speed up the process. 这样做的想法是添加/删除这些统计信息,而无需重新绘制散点图,从而加快流程。 So far I can add plots independently on the figure but I can't delete a specific plot. 到目前为止,我可以在图上独立添加图,但是不能删除特定图。 When I uncheck the checkbox, I get the following error: 取消选中该复选框时,出现以下错误:

AttributeError: 'Graphics' object has no attribute 'vline1'

I understand that when I create the plot, I need to store/return the plot in order to call it later when I want to delete it but I don't know how to do that. 我知道在创建图时,我需要存储/返回图,以便以后在要删除它时调用它,但我不知道该怎么做。

Here my current code: 这是我当前的代码:

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

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

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)
        self.gridLayout = QGridLayout(centralWidget)
        self.gridLayout.addWidget(self.canvas)   
        self.btn_plot = QCheckBox("Plot")
        self.btn_line = QCheckBox("Line")
        self.gridLayout.addWidget(self.btn_plot, 1,0,1,1)
        self.gridLayout.addWidget(self.btn_line, 2,0,1,1)
        self.btn_plot.clicked.connect(self.btnPlot)
        self.btn_line.clicked.connect(self.btnLine)

    def btnPlot(self):
        self.checked = self.btn_plot.isChecked()
        self.Graphics = Graphics('plot', self.checked, self.axes)

    def btnLine(self):
        self.checked = self.btn_line.isChecked()
        self.Graphics = Graphics('line', self.checked, self.axes)

class Graphics:
    def __init__(self, typeGraph, checked, axes):
        self.typeGraph = typeGraph
        self.checked = checked
        self.axes = axes
        if self.typeGraph == 'plot': self.drawPlot()
        if self.typeGraph == 'line': self.drawLine()

    def drawPlot(self):
        if self.checked == True:
            self.plot = self.axes.plot([10,20,30], [5,10,2], 'o')
        else:
            self.plot.remove()
        self.axes.figure.canvas.draw()

    def drawLine(self):
        if self.checked == True:
            self.vline1 = self.axes.axvline(x=15, linestyle="dashed", color="#595959")
            self.vline2 = self.axes.axvline(x=25, linestyle="dashed", color="#595959")
        else:
            self.vline1.remove()
            self.vline2.remove()
        self.axes.figure.canvas.draw()

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

Problem is be because when you click it then it creates always new Graphics (in btnPlot / btnLine ) which doesn't have previous values - plot, vline1, vline2 . 问题是因为当您单击它时,它将始终创建不具有先前值的新Graphics (在btnPlot / btnLineplot, vline1, vline2 You have to create Graphics only once and later run only drawPlot(checked) , drawLine(checked) to add or remove item. 您只需创建一次Graphics仅运行drawPlot(checked)drawLine(checked)即可添加或删除项目。

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

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

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)
        self.gridLayout = QGridLayout(centralWidget)
        self.gridLayout.addWidget(self.canvas)   
        self.btn_plot = QCheckBox("Plot")
        self.btn_line = QCheckBox("Line")
        self.gridLayout.addWidget(self.btn_plot, 1,0,1,1)
        self.gridLayout.addWidget(self.btn_line, 2,0,1,1)
        self.btn_plot.clicked.connect(self.btnPlot)
        self.btn_line.clicked.connect(self.btnLine)

        # create only once
        self.Graphics = Graphics(self.axes)

    def btnPlot(self):
        # add or remove 
        self.Graphics.drawPlot(self.btn_plot.isChecked())

    def btnLine(self):
        # add or remove 
        self.Graphics.drawLine(self.btn_line.isChecked())

class Graphics:
    def __init__(self, axes):
        self.axes = axes
        # create at start with default values (but frankly, now I don't need it)
        self.plot = None
        self.vline1 = None
        self.vline2 = None

    def drawPlot(self, checked):
        if checked:
            self.plot = self.axes.plot([10,20,30], [5,10,2], 'o')
        else:
            for item in self.plot:
                item.remove()
        self.axes.figure.canvas.draw()

    def drawLine(self, checked):
        if checked:
            self.vline1 = self.axes.axvline(x=15, linestyle="dashed", color="#595959")
            self.vline2 = self.axes.axvline(x=25, linestyle="dashed", color="#595959")
        else:
            self.vline1.remove()
            self.vline2.remove()
        self.axes.figure.canvas.draw()

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    prog = Mainwindow()   
    prog.show()
    sys.exit(app.exec())

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

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