Python 获取Matplotlib单选按钮的状态

Python 获取Matplotlib单选按钮的状态,python,matplotlib,scipy,Python,Matplotlib,Scipy,使用Matplotlib,我可以通过使用“mySlider.val”获取滑块小部件的值,这非常方便。是否有同等能力获取当前选择的单选按钮?我认为这个问题正是我们想要问的,但是提问者没有提供一个有效的例子。我提供了下面的示例,指出了我正在寻找的代码的缺失行 from matplotlib.widgets import RadioButtons, Button, Slider import matplotlib.pyplot as plt axSlider = plt.axes([0.1, 0.8

使用Matplotlib,我可以通过使用“mySlider.val”获取滑块小部件的值,这非常方便。是否有同等能力获取当前选择的单选按钮?我认为这个问题正是我们想要问的,但是提问者没有提供一个有效的例子。我提供了下面的示例,指出了我正在寻找的代码的缺失行

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()
以下是输出结果:


如果这是不可能的,你能建议一个优雅的工作周围?目前,我正在为单选按钮使用“点击”功能,并使用单选按钮的值设置全局变量,这很混乱。

正如@tcaswell所建议的,我在github上向matplotlib提交了一份报告,现在可以查询单选按钮的当前状态。缺少的代码行是:

radioValue = butRadio.value_selected
这将被纳入matplotlib的1.5.x版本中。同时,如果您想使用此功能,则需要下载的开发版本(或者至少下载“widgets.py”文件)


现在已将其纳入当前版本的Matplotlib(1.5.3)中。(请参阅。)如果上面这一行不起作用,您可能只需要更新Matplotlib。

刚刚偶然发现这篇文章对同一问题进行了研究。他们还没有对matplotlib进行任何更新,尽管我在这里找到了一篇关于从matplotlib的复选按钮检索值的相关文章:

总结一下答案,就是保留一个
dict
,其中为每个按钮存储一个变量。每当按下按钮时,让事件处理程序更改
dict
中的必要值,然后其他函数可以根据需要访问这些值,而不是仅在事件处理程序中捕获这些值


希望这有帮助!我知道我不想玩弄matplotlib的源代码,最简单的答案是:

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()
从matplotlib.widgets导入单选按钮、按钮、滑块
将matplotlib.pyplot作为plt导入
axSlider=plt.轴([0.1,0.8,0.4,0.05])
aSlider=滑块(axSlider,'A Slider',0,1,valinit=0.5)
axRadio=plt.轴([0.1,0.2,0.3,0.4])
我的列表=(‘顶部’、‘中间’、‘底部’)
butRadio=单选按钮(axRadio,我的列表)
axStatus=plt.轴([0.5,0.2,0.3,0.4])
b状态=按钮(axStatus,'Get status of radio buttons')
def激活单选按钮(x,列表):
对于x范围内的i(len(x.circles)):

如果x.circles[i].get_facecolor()[0],如果您查看
单选按钮的源代码,它不会跟踪这一点。您只需为当前活动的按钮添加一个实例属性,并在
\u单击
()中更新它。这似乎是它应该具有的功能,并且欢迎使用PR来添加它。谢谢您的建议!我在widgets.py中添加了两行代码,似乎实现了这个目标。具体来说,我将
self.val=t.get_text()
添加到
\u单击的
函数中,并将
self.val=label
添加到
\u初始化
函数中。我创建了matplotlib github的pull请求,但这是我第一次这样做,所以希望我做得正确。谢谢这是合并的,将与1.5(即将发布)一起发布。我肯定不是,但我知道在我使用Python的noobish级别上,我不想触碰它,所以我的答案可能会帮助我级别上的其他人。现在我只希望他们能给matplotlib添加一些下拉菜单。。。