Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 705
DanHickstein Avatar asked Jan 05 '15 17:01

DanHickstein


2 Answers

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.

like image 185
DanHickstein Avatar answered Oct 06 '22 15:10

DanHickstein


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()
like image 43
Daniel Ariad Avatar answered Oct 06 '22 13:10

Daniel Ariad