将复选按钮重置为matplotlib中的原始值

2024-10-01 07:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个滑块、一个带有两个选项的复选框(设置为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)

在重置例程中,我使用以下命令重置滑块和复选按钮:

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

问题在于上面的CheckPar.set_active(0)切换第一个复选框的值。我想要的是将两个复选框重置为“False”的原始值

这在Tkinter和Java中似乎是可行的,但我无法用matplotlib实现。matplotlib文档说明如何使用

set_active(self, index)

通过索引直接(取消)激活复选按钮,其中索引是原始标签列表中的索引

不幸的是,我不知道如何实现这一点,我也找不到任何有人这样做的例子。我尝试过的事情包括:

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

最后两个选项会导致类型错误

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

我可以使用

status = CheckPar.get_status()

但我无法将它们重置为原始值

任何帮助都将不胜感激

谢谢


Tags: falseonplt按钮复选框active重置reset
1条回答
网友
1楼 · 发布于 2024-10-01 07:24:14

您必须知道处于原始状态的框的状态,然后遍历这些框,并且仅当当前状态与原始状态不匹配时才切换它们的状态

请参阅此测试:

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()

相关问题 更多 >