我有两个CheckButtons小部件,每个小部件有3个元素。当选择其中一个CheckButtons时,我想读取两个小部件的状态,然后相应地更新图表。
滑块小部件有一个.val
用于返回滑块的状态,但CheckButtons小部件看起来有点尴尬(或者我错过了一些明显的东西)?
简短的例子:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
class Example:
def updateChart(self, event):
colour = self.colours.labels # gets labes as text object, is there an easy way of getting the status?
print colour
# measurement = measurements.something
def __init__(self):
colourax = plt.axes([0.5, 0.4, 0.09, 0.2])
measurementax = plt.axes([0.5, 0.6, 0.09, 0.2])
self.colours = CheckButtons(colourax, ('Red', 'Green', 'Blue'), (False, False, False))
self.measurements = CheckButtons(measurementax, ('1', '2', '3'), (False, False, False))
self.colours.on_clicked(self.updateChart)
self.measurements.on_clicked(self.updateChart)
def run(self):
plt.show()
ex = Example()
ex.run()
答案 0 :(得分:6)
我知道这有点尴尬,但你可以检查复选框中十字线的可见性。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
colourax = plt.axes([0.5, 0.4, 0.09, 0.2])
colours = CheckButtons(colourax, ('Red', 'Green', 'Blue'), (False, False, False))
isRedChecked = colours.lines[0][0].get_visible()
isGreenChecked = colours.lines[1][0].get_visible()
isBlueChecked = colours.lines[2][0].get_visible()
答案 1 :(得分:2)
目前的开发版本(截至2017年7月)有一个
CheckButtons.get_status()
方法合并。这可用于查询复选框的当前状态。它应该很快就会在稳定版本中发布。 (Source here)
在此之前,您可以使用自己的get_status
方法来模拟此行为,如下所示。它使用与开发版本中的get_status()
方法相同的机制,这也非常接近@Gruby提出的答案(查看线条的可见性)。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
class Example:
def updateChart(self, event):
colour = self.get_status(self.colours)
measurement = self.get_status(self.measurements)
print measurement, colour
def get_status(self, cb):
return [l1.get_visible() for (l1, l2) in cb.lines]
def __init__(self):
colourax = plt.axes([0.5, 0.4, 0.09, 0.2])
measurementax = plt.axes([0.5, 0.6, 0.09, 0.2])
self.colours = CheckButtons(colourax, ('Red', 'Green', 'Blue'), (False, False, False))
self.measurements = CheckButtons(measurementax, ('1', '2', '3'), (False, False, False))
self.colours.on_clicked(self.updateChart)
self.measurements.on_clicked(self.updateChart)
def run(self):
plt.show()
ex = Example()
ex.run()
答案 2 :(得分:1)
可能有更优雅的方式,但您可以随时跟踪每个复选框的状态,例如在dict
中。您使用on_clicked()
指定的函数将接收活动复选框的标签字符串作为其第二个参数,然后您可以使用它来相应地更新状态:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
class Example:
def onColor(self,label):
self.cstates[label] = not self.cstates[label]
print 'un'*(not self.cstates[label]) + 'checked %s' %label
self.updateChart()
def onMeasurement(self,label):
self.mstates[label] = not self.mstates[label]
print 'un'*(not self.mstates[label]) + 'checked %s' %label
self.updateChart()
def updateChart(self, event=None):
"""do something here using self.cstates and self.mstates?"""
pass
def __init__(self):
colourax = plt.axes([0.5, 0.4, 0.09, 0.2])
measurementax = plt.axes([0.5, 0.6, 0.09, 0.2])
clabels, cvals = ('Red', 'Green', 'Blue'), (False,)*3
mlabels, mvals = ('1', '2', '3'), (False,)*3
self.cstates = dict(zip(clabels,cvals))
self.mstates = dict(zip(mlabels,mvals))
self.colours = CheckButtons(colourax, clabels, cvals)
self.colours.on_clicked(self.onColor)
self.measurements = CheckButtons(measurementax, mlabels, mvals)
self.measurements.on_clicked(self.onMeasurement)
def run(self):
plt.show()
ex = Example()
ex.run()
不是最漂亮的,但它确实有效!