使用交互改变另一个参数时重置一个参数

Reset an argument when varying another argument using interact

我想知道 ipywidgets 是否支持在您 manipulate/vary/slide/change 其他交互式参数之一时将其中一个参数重置为默认值。以他们的 documentation

为例
%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

def f(m, b):
    plt.figure(2)
    x = np.linspace(-10, 10, num=1000)
    plt.plot(x, m * x + b)
    plt.ylim(-5, 5)
    plt.show()

interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot

我要寻找的是一种在我更改 m 时将参数 b 重置为某个默认值的方法。这是否受支持,如果不支持,是否有人能够想出一个聪明的方法来做到这一点?我个人不能。

这是一个相当简单的示例,但是您可以将 default_value 定义为 b 的普通实例变量,然后在 m 通过普通 [=] 更改时重新分配它14=]命令。

在这种情况下,您有两个或多个小部件进行交互,可能更容易管理为 class,其中两个输入是实例变量,特别是如果您想在多个小部件中使用这种小部件组合位置相同 module/notebook.

%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

def f(m, b):
    plt.figure(2)
    x = np.linspace(-10, 10, num=1000)
    plt.plot(x, m * x + b)
    plt.ylim(-5, 5)
    plt.show()

interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'

m = interactive_plot.children[0]
b = interactive_plot.children[1]
b.default_value = 0 

def set_b_default(button):
    b.value = b.default_value

m.observe(set_default)

interactive_plot