Getting selected values ​​from CheckButtons object in matplotlib

I have two CheckButtons widgets with 3 elements each. I would like to read the status of both widgets when one of the CheckButtons is selected, and update the chart accordingly.

Does the slider widget have a .val to return the slider status, but does the CheckButtons widget seem a little more uncomfortable (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() 
+3
source share
3 answers

There may be a more elegant way, but you can always monitor the status of each of these flags yourself, for example. in dict . The function that you specify with on_clicked() will receive the label of the active cell as the second argument, which you can then use to update the state accordingly:

 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 most beautiful, but it works!

+1
source

I know this is a bit uncomfortable, but you can check the visibility of the cross lines in the fields.

 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() 
+5
source

The current development version (as of July 2017) has

 CheckButtons.get_status() 

. This can be used to query the current state of the flags. It should be released in a stable version pretty soon. ( Source here )

Until then, you can emulate this behavior using your own get_status method, as shown below. It uses the same mechanism as the get_status() method from the development version, which is also very close to what @Gruby's answer suggests (depending on the visibility of the lines).

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

Source: https://habr.com/ru/post/1210417/


All Articles