DanHickstein DanHickstein - 11 days ago 6
Python Question

Get status of Matplotlib radio button

Using Matplotlib, I can get the value of a Slider widget by using "mySlider.val", which is very convenient. Is there an equivalent capability to get the current selection of a Radio Button? I think that this question is what this question was trying to ask, but the asker did not provide a working example. I provide the following example, indicating the missing line of code that I am looking for.

from matplotlib.widgets import RadioButtons, Button, Slider
import matplotlib.pyplot as plt

axSlider = plt.axes([0.1, 0.8, 0.4, 0.05])
aSlider = Slider(axSlider,'A slider', 0,1, valinit=0.5)

axRadio = plt.axes([0.1, 0.2, 0.3, 0.4])
butRadio = RadioButtons(axRadio, ('Top','Middle','Bottom'))

axStatus = plt.axes([0.5, 0.2, 0.3, 0.4])
bStatus = Button(axStatus,'Get status of radio buttons')

def get_status(val):
#radioValue = ???? -- Need this line of code.
radioValue = 0
sliderValue= aSlider.val
print 'Slider value: %.2f, Radio button value: %.2f'%(sliderValue,radioValue)

bStatus.on_clicked(get_status)
plt.show()


Here is what the output looks like:

enter image description here

If this is not possible, can you suggest an elegant work-around? Currently I am using the "on_clicked" function for the Radio Buttons and setting global variables with the values of the radio buttons, and this is messy.

Answer

As suggested by @tcaswell, I submitted a pull request to matplotlib on github and now it is possible to query the current status of the RadioButtons. The missing line of code is:

radioValue = butRadio.value_selected

This is scheduled to be incorporated into the 1.5.x release of matplotlib. In the mean time, if you want to use this feature, you will need to download the development release of matplotlib from github (or, at least download the 'widgets.py' file).

This is now incorporated into the current release of Matplotlib (1.5.3). (See the Widets Docs.) If this above line doesn't work, you can likely just update Matplotlib.