Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - Retrieving the selected values from a CheckButtons object in matplotlib

I have two CheckButtons widgets with 3 elements each. I'd like to read the status of both widgets when either one of the CheckButtons is selected then update the chart accordingly.

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

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...