【问题标题】:Reset CheckButtons to original values in matplotlib在 matplotlib 中将 CheckButtons 重置为原始值
【发布时间】:2019-12-11 13:36:21
【问题描述】:

我有几个滑块、一个带有两个设置为 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)

在我的重置例程中,我使用以下内容来重置我的滑块和 CheckButtons:

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

问题出在上面,CheckPar.set_active(0) 切换了 FIRST 复选框的值。我想要的是将两个复选框重置为“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()

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

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: python matplotlib matplotlib-widget


    【解决方案1】:

    你必须知道盒子在原始状态下的状态,然后遍历盒子,只有当当前状态与原始状态不匹配时才切换它们的状态。

    看这个测试:

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

    【讨论】:

    • 非常好!效果很好!!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2015-07-10
    相关资源
    最近更新 更多