简体   繁体   English

在 matplotlib 中将 CheckButtons 重置为原始值

[英]Reset CheckButtons to original values in matplotlib

I have a couple of sliders, a checkbox with two options that I set to False, and a reset button, as follows:我有几个滑块,一个带有两个选项的复选框,我设置为 False,还有一个重置按钮,如下所示:

#Slider
slider_M = plt.axes(rectM, facecolor=axcolor)   
svalueM = Slider(slider_M, 'M', s_Mmin, s_Mmax, valinit=s_Minit, valfmt="%1.2f")
svalueM.on_changed(updateChart) 

#CheckBox
ShkBox = plt.axes(rectC,facecolor=axcolor)
CheckPar = CheckButtons(ShkBox, ('Strong', 'Weak'), (False, False)) 
CheckPar.on_clicked(updateChart)

#Reset Button
resetax = plt.axes(rectR, facecolor=axcolor)
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(resetplt)

In my reset routine I use the following to reset my slider and CheckButtons:在我的重置例程中,我使用以下内容重置我的滑块和 CheckButtons:

def resetplt(event)
    svalueM.reset()   #Reset Slider
    CheckPar.set_active(0)  # Reset Checkbox

The issue is with the above, CheckPar.set_active(0) toggles the value of the FIRST checkbox.问题出在上面, CheckPar.set_active(0) 切换 FIRST 复选框的值。 What I want is to reset both checkboxes to the original value of "False".我想要的是将两个复选框重置为“False”的原始值。

This appears doable in Tkinter and Java but I am unable to implement with matplotlib.这在 Tkinter 和 Java 中似乎可行,但我无法使用 matplotlib 实现。 The matplotlib documentation says to use matplotlib 文档说要使用

set_active(self, index)

to Directly (de)activate a check button by index, where index is an index into the original label list.按索引直接(取消)激活检查按钮,其中索引是原始标签列表的索引。

Unfortunately, I do not know how to implement this and I am unable to find any examples of where someone did.不幸的是,我不知道如何实现这一点,我无法找到任何关于某人所做的例子。 Things I've tried include:我尝试过的事情包括:

CheckPar.set_active(0)
CheckPar.set_active(("Strong","Weak"),(0, 0))
CheckPar.set_active([0],[1])

The last two options result in errors of the type最后两个选项导致类型错误

TypeError: set_active() takes 2 positional arguments but 3 were given

I am able to determine the status of the boxes using我能够使用确定框的状态

status = CheckPar.get_status()

But I cannot reset them to their original value.但我无法将它们重置为原始值。

Any help would be appreciated.任何帮助,将不胜感激。

Thanks谢谢

You have to know the state of the boxes in their original state, then iterate through the boxes, and switch their state only if the current state does not match the original one.你必须知道盒子在它们的原始状态下的状态,然后遍历盒子,只有当当前状态与原始状态不匹配时才切换它们的状态。

See this test:看这个测试:

def resetplt(event):
    for i, (state, orig) in enumerate(zip(CheckPar.get_status(), origState)):
        if not state == orig:
            CheckPar.set_active(i)  # switch state


fig, (ax1, ax2) = plt.subplots(1, 2)

# CheckBox
origState = (False, True)
ShkBox = plt.axes(ax1)
CheckPar = CheckButtons(ShkBox, ('Strong', 'Weak'), origState)

# Reset Button
resetax = plt.axes(ax2)
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(resetplt)

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM