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

2024-06-28 20:58:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个checkbutton小部件,每个小部件有3个元素。我想在选中其中一个复选按钮时读取两个小部件的状态,然后相应地更新图表。在

slider小部件有一个.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()

Tags: importselffalsematplotlib部件example状态def
3条回答

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

CheckButtons.get_status()

合并方法。这可用于查询复选框的当前状态。它应该很快在稳定版本中发布。(Source here

在此之前,您可以使用您自己的get_status方法来模拟这种行为,如下所示。它使用与开发版本中的get_status()方法相同的机制,这也非常接近@Gruby的答案(查看行的可见性)。在

^{pr2}$

也许有一种更优雅的方法,但是你可以自己跟踪每个复选框的状态,例如在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()

不是最漂亮的,但很管用!在

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

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

相关问题 更多 >