简体   繁体   English

当使用 PyQt5 检查 QCheckBox 时,显示 matplotlib plot

[英]show matplotlib plot when QCheckBox is checked with PyQt5

I want to make a Gui using PyQt5 and show matplotlib plots.我想使用 PyQt5 制作一个 Gui 并显示 matplotlib 图。
Here is a picture of the main window:下面是主window的图片:

在此处输入图像描述 The question is that is there any way to show a plot when it's QCheckBox is checked?问题是有什么方法可以在检查 QCheckBox 时显示 plot 吗? I have inserted 4 checkboxes and each one has different plots or different options.我插入了 4 个复选框,每个复选框都有不同的图或不同的选项。 for example 2 of them are going to show 2 different plots and two of them are going to change the axis into log coordinates.例如,其中 2 个将显示 2 个不同的图,其中两个将轴更改为对数坐标。
I know that in the toolbar there is an option to do that but I want to do these changes instantly by clicking on checkboxes.我知道在工具栏中有一个选项可以做到这一点,但我想通过单击复选框立即进行这些更改。
I have found this using PyQt4 but it is using a QPushhButton for changes.我使用 PyQt4 发现了这一点,但它使用 QPushhButton 进行更改。 I desire instantly changes.我渴望立即改变。
And matplotlib's widgets andthis example are not appropriate for my Gui.而 matplotlib 的小部件和这个例子不适合我的 Gui。
Any suggestion?有什么建议吗?

Here is the code and .ui file in qt designer.这是 qt 设计器中的代码和.ui 文件 Maybe some codes are more than enough because I was trying so many things.也许一些代码就足够了,因为我尝试了很多东西。 two sample list for x and y. x 和 y 的两个样本列表。

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib
matplotlib.use('Qt5Agg')

ui, _ = loadUiType('plots.ui')

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)


class MainWindow(QMainWindow, ui):
    def __init__(self, *args, **kwargs):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.checkBox_1.stateChanged.connect(self.checker)
        self.checkBox_2.stateChanged.connect(self.checker)

        toolbar = NavigationToolbar(sc, self)
        self.verticalLayout.addWidget(toolbar)
        self.verticalLayout.addWidget(sc)

    def checker(self):
        x = [1,2,3,4,5]
        y = [1,2,3,4,5]
        if self.checkBox_1.isChecked():
            sc = MplCanvas(self, width=5, height=4, dpi=100)
            sc.axes.plot(x, y)
            self.show()

        if self.checkBox_2.isChecked():
            sc = MplCanvas(self, width=5, height=4, dpi=100)
            sc.axes.plot(x, y)
            self.show()

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()

After working hard on the subject I finally have found the solution.在努力研究这个问题后,我终于找到了解决方案。 but maybe it is coded too much I still can get suggestions from others.但也许它编码太多我仍然可以从其他人那里得到建议。
Here is what I have tried:这是我尝试过的:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib
matplotlib.use('Qt5Agg')

ui, _ = loadUiType('plots.ui')

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)


class MainWindow(QMainWindow, ui):
    def __init__(self, *args, **kwargs):
        QMainWindow.__init__(self)
        self.setupUi(self)

        self.checkBox_1.stateChanged.connect(self.raw)
        self.checkBox_2.stateChanged.connect(self.raw)

    def raw(self):
        for i in range(self.verticalLayout.count()): self.verticalLayout.itemAt(i).widget().close()
        x = [1,2,3,4,5]
        y = [6,8,2,7,3]
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        toolbar = NavigationToolbar(sc, self)
        self.verticalLayout.addWidget(toolbar)
        self.verticalLayout.addWidget(sc)
        if self.checkBox_1.isChecked() and not self.checkBox_2.isChecked():
            sc.axes.plot(x, y)
        if self.checkBox_2.isChecked() and not self.checkBox_1.isChecked():
            sc.axes.plot(y, x)
        elif self.checkBox_2.isChecked() and self.checkBox_1.isChecked():
            sc.axes.plot(x, y, 'r', y, x, 'b')
        self.show()


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()

As you can see in this code I have tried to clear the layout every time the checkboxes are changed in their state and an if statement for plots.正如您在这段代码中看到的那样,每次在 state 和绘图的if statement中更改复选框时,我都尝试清除布局。
I know there is a better for doing this so I can use your valuable suggestions.我知道这样做有更好的方法,因此我可以使用您的宝贵建议。 在此处输入图像描述

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

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