简体   繁体   English

从matplotlib中的CheckButtons对象检索选定的值

[英]Retrieving the selected values from a CheckButtons object in matplotlib

I have two CheckButtons widgets with 3 elements each. 我有两个每个都有3个元素的CheckButtons小部件。 I'd like to read the status of both widgets when either one of the CheckButtons is selected then update the chart accordingly. 当选择任一CheckButtons时,我想读取两个小部件的状态,然后相应地更新图表。

The slider widget has a .val for returning the status of a slider, but the CheckButtons widget seems a bit more awkward (or am I missing something obvious)? 滑块小部件具有用于返回滑块状态的.val ,但是CheckButtons小部件似乎更尴尬(或者我缺少明显的东西)吗?

short example: 简短的例子:

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()

I know it's a bit awkward, but you can check for visibility of on of the cross lines in check boxes. 我知道这有点尴尬,但是您可以检查复选框中交叉线的可见性。

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()

The current development version (as of July 2017) has a 当前的开发版本(截至2017年7月)具有一个

CheckButtons.get_status()

method incorporated. 方法合并。 This can be used to query the current status of the checkboxes. 这可用于查询复选框的当前状态。 It should be released in the stable version pretty soon. 它应该很快以稳定版本发布。 ( Source here ) 这里的来源

Until then, you may emulate this behaviour by using your own get_status method as in the following. 在此之前,您可以使用自己的get_status方法模仿此行为,如下所示。 It uses the same mechanism as the get_status() method from the development version, which is also very close to what the answer of @Gruby is proposing (looking at the visibility of the lines). 它使用与开发版本中的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()

There might perhaps be a more elegant way but you can always keep track of the states of each of the checkboxes yourself, eg in a dict . 也许有一种更优雅的方法,但是您始终可以自己跟踪每个复选框的状态,例如在dict The function that you specify using on_clicked() will receive the label string of the active checkbox as its second argument, which you can then use to update the status appropriately: 您使用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()

Not the prettiest, but it works! 不是最漂亮的,但它有效!

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

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