Get Matplotlib Switch Status

Using Matplotlib, I can get the value of the Slider widget using "mySlider.val", which is very convenient. Is there an equivalent opportunity to get the current selection of the Radio button? I think this question is what this question was trying to ask, but the searcher did not provide a working example. I provide the following example pointing to the missing line of code I'm 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 the result:

enter image description here

If this is not possible, can you offer an elegant detour? I am currently using the "on_clicked" function for the Radio buttons and setting global variables with switch values, and this is dirty.

+5
source share
3 answers

As @tcaswell suggested, I sent a pull request to matplotlib on github, and now you can request the current status of the Radio button. Invalid line of code:

 radioValue = butRadio.value_selected 

This is planned to be included in the release of matplotlib version 1.5.x. At the same time, if you want to use this function, you will need to download the matplotlib release from github (or at least download the widgets.py file).

It is now included in the current version of Matplotlib (1.5.3). (See Widets Docs .) If this line above does not work, you can most likely upgrade Matplotlib.

+4
source

The simplest answer:

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

Just stumbled upon this post while doing research on the same issue. They have not updated matplotlib yet, although I found a related post on fetching values ​​from matplotlib control buttons here: Getting selected values ​​from CheckButtons object in matplotlib

To summarize the answer, you need to save the dict in which you store the variable for each button. Whenever a button is pressed, the event handler changes the necessary values ​​in the dict , which can then be accessed by other functions as necessary, and not the values ​​that fall into the event handler alone.

Hope this helps! I know that it is better not to play with matplotlib source code.

0
source

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


All Articles