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:
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.
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.
The simplest answer is:
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])
my_list = ('Top','Middle','Bottom')
butRadio = RadioButtons(axRadio, my_list)
axStatus = plt.axes([0.5, 0.2, 0.3, 0.4])
bStatus = Button(axStatus,'Get status of radio buttons')
def activated_radio_button(x,list):
for i in xrange(len(x.circles)):
if x.circles[i].get_facecolor()[0]<.5:
return list[i]
def get_status(label):
radioValue = activated_radio_button(butRadio,my_list)
sliderValue= aSlider.val
print 'Slider value: %.2f, Radio button value: %s'%(sliderValue,radioValue)
bStatus.on_clicked(get_status)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With