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

Posted

技术标签:

【中文标题】在 Matplotlib 图上独立添加/删除图【英文标题】:Add/Delete plots independently on a Matplotlib figure 【发布时间】:2019-08-14 23:20:57 【问题描述】:

我想生成一个散点图(最多 50 万个点),然后添加不同的统计数据(例如 Q1、中位数、Q3)。我们的想法是在不重新绘制散点图的情况下添加/删除这些统计数据,以加快处理速度。到目前为止,我可以在图中独立添加图,但不能删除特定图。 当我取消选中该复选框时,我收到以下错误:

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

我了解当我创建情节时,我需要存储/返回情节以便稍后在我想删除它时调用它,但我不知道该怎么做。

这是我当前的代码:

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

【问题讨论】:

哪一行有问题? 一些关于从图中删除绘图的信息***.com/questions/4981815/… 问题可能是因为当您单击它时,它总是会创建没有先前值的新图形(在 btnPlot/btnLine 中) - 您应该只创建一次图形。 【参考方案1】:

问题在于,当您单击它时,它总是会创建新的 Graphics(在 btnPlot/btnLine 中),它没有以前的值 - plot, vline1, vline2。您只需创建一次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())

【讨论】:

以上是关于在 Matplotlib 图上独立添加/删除图的主要内容,如果未能解决你的问题,请参考以下文章

python使用matplotlib绘制水平条形图并在条形图上添加实际数值标签实战

删除 matplotlib 图上的图例

如何在matplotlib中的给定图上绘制垂直线

matplotlib:在条形图上绘制多列熊猫数据框

如何在 matplotlib 条形图上旋转 x 轴刻度标签?尝试了几种方法,都没有奏效

如何在 matplotlib 条形图上旋转 x 轴刻度标签?尝试了几种方法,都没有奏效