我正在尝试使用Jupyter Notebook中的ipywidgets库创建几个交互式图。然而,我面临一个问题:当一个输入窗口小部件发生变化时,我会尝试更改多个绘图,例如滑块。
在添加的MWE中,这用正弦和余弦函数说明,它们都依赖于单个输入小部件来设置正弦和余弦函数的幅度。因此,如果输入滑块的值设置为值2,则正弦和余弦函数的幅度必须紧随其后。
如果您自己运行代码,那么您将看到两个滑块完美地一起移动。但是,并非两个图都相应地同时改变。
有人有解决方案吗?不一定用ipywidgets?
提前谢谢你,
瑞克
# FIRST JUPYTER NOTEBOOK CELL
import matplotlib.pyplot as plt
from ipywidgets import *
from math import *
import numpy as np
%matplotlib inline
default_value = 1 # Default value of amplutide
a = IntSlider(min = 1, max = 10, value = default_value, description = 'Amplitude of sine function')
b = IntSlider(min = 1, max = 10, value = default_value, description = 'Amplitude of sine function')
mylink = jslink((a, 'value'), (b, 'value'))
def widget_sine_function(amp_s = a):
x = np.linspace(0,20,100000) # Creat x-values for sine function
y = [amp_s*sin(i) for i in x] # Create y-values for sine function with amplitude according to value of widget a
plt.clf()
plt.figure(figsize=(15,5))
plt.subplot(1, 2, 1)
plt.plot(x,y)
interact(widget_sine_function)
# SECOND JUPYTER NOTEBOOK CELL
def widget_cosine_function(amp_c = b):
x = np.linspace(0,20,100000) # Creat x-values for cosine function
y = [amp_c*cos(i) for i in x] # Create y-values for cosine function with amplitude according to value of widget b
plt.clf()
plt.figure(figsize=(15,5))
plt.subplot(1, 2, 1)
plt.plot(x,y)
interact(widget_cosine_function)