【问题标题】:Why aren't the matplotlib checkboxes working in pyQt5?为什么 matplotlib 复选框在 pyQt5 中不起作用?
【发布时间】:2022-01-24 17:05:35
【问题描述】:

我的代码中有一个小问题,因为我有一个带有复选框的 matplotlib 图(用于选择要绘制的内容),但是当我用 Pyqt5(一个按钮)打开它时,它确实打开了,但复选框不起作用,我们不能触摸它们。我的功能运行良好,但不适用于 Pyqt5,我希望我足够精确,如果我不够精确,我很抱歉。如果可以帮助您,这是我的代码:

from PyQt5.QtWidgets import QPushButton, QMainWindow, QApplication
import sys
from matplotlib.widgets import CheckButtons
import matplotlib.pyplot as plt


def graph_check () :
    x = [1,2,3,4,5,6]
    y1 = [1,1,1,1,3,1]
    y2 = [0,2,1,2,2,1]
    y3 = [4,3,2,0,0,5]
    fig,ax = plt.subplots()
    p1, = ax.plot(x,y1,color = 'red', label = 'red')
    p2, = ax.plot(x,y2,color = 'green', label = 'green')
    p3, = ax.plot(x,y3,color = 'blue', label = 'blue')
    lines = [p1,p2,p3]
    plt.subplots_adjust(left = 0.25, bottom=0.1, right=0.95,top = 0.95)
    # checkbuttons widgets
    labels = ['red', 'green', 'blue']
    activated = [True, True,True]
    axCheckbutton = plt.axes([0.03,0.4,0.15,0.15])
    chxbox = CheckButtons(axCheckbutton, labels,activated)
    def set_visible (label) :
        index = labels.index(label)
        lines[index].set_visible(not lines[index].get_visible())
        plt.draw()
    chxbox.on_clicked(set_visible)
    plt.show()
    # that function does work well, in the end we have a graph with 3 lines and we can make   
    # them visible or not thanks to the checkbox.

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 600, 400)
        self.btn1 = QPushButton('Graph check', self)
        self.btn1.setGeometry(130, 215, 125, 55)
        self.btn1.clicked.connect(self.btn1_onClicked)
        self.show()

    def btn1_onClicked(self):
        graph_check()
        # it works, we can see the graph but it is impossible to use the checkbox...


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

【问题讨论】:

  • 我无法重现该问题,因为我可以切换所有三个复选框,并且图表可以正确切换线条。请添加有关您的配置的更多详细信息:PyQt 版本 (from PyQt5 import QtCore; print(QtCore.QT_VERSION_STR))、matplotlib 版本 (import matplotlib; print(matplotlib.__version__)) 和操作系统。
  • 首先感谢您的回答。然后,我按照您的要求完成了这些结果:对于 QtCore.QT_VERSION_STR,我有“5.15.2”,对于 matplotlib 版本,它返回:“3.5.1”。圣诞快乐
  • @musicamante 我确实重现了这个问题,您使用的是特定环境吗?
  • @eyllanesc 我无法在 Linux 上使用 Qt 5.15.2 和 matplotlib 3.3.4 重现它(意味着它可以按预期工作),只需将上面的代码复制到脚本并从终端。据我所知,plot.show() 启动一个新的事件循环,如果存在且未运行 创建另一个事件循环,execs 它,然后阻塞直到绘图小部件关闭,所以对复选框的引用不是垃圾收集。也许在更高版本的 matplotlib 中行为发生了变化?

标签: python matplotlib plot checkbox pyqt5


【解决方案1】:

这个问题是因为“chxbox”是一个局部变量,当函数执行完成时,它的引用将被删除。一种可能的解决方案是使其成为另一个具有更大范围的对象的属性:

# ...
plt.chxbox = CheckButtons(axCheckbutton, labels, activated)

def set_visible(label):
    index = labels.index(label)
    lines[index].set_visible(not lines[index].get_visible())
    plt.draw()

plt.chxbox.on_clicked(set_visible)
# ...

【讨论】:

    猜你喜欢
    • 2015-04-11
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2021-02-10
    • 2016-01-14
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多