4

我有两个 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()
4

3 回答 3

7

我知道这有点尴尬,但您可以检查复选框中交叉线的可见性。

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()
于 2014-03-21T15:50:45.207 回答
2

当前的开发版本(截至 2017 年 7 月)具有

CheckButtons.get_status()

方法并入。这可用于查询复选框的当前状态。它应该很快就会在稳定版本中发布。(来源在这里

在此之前,您可以使用您自己的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()
于 2017-07-13T09:25:41.973 回答
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()

不是最漂亮的,但它有效!

于 2013-07-23T15:38:33.283 回答