为什么 matplotlib 复选框在 pyQt5 中不起作用?
Why aren't the matplotlib checkboxes working in pyQt5?
我的代码有一个小问题,因为我有一个带复选框的 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())
问题是因为“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)
# ...
我的代码有一个小问题,因为我有一个带复选框的 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())
问题是因为“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)
# ...