更新时间:2023-11-30 16:01:28
This can be done by leveraging the interactive function.
%pylab inline
from ipywidgets import widgets
from IPython.display import display
import numpy as np
import matplotlib
t = np.arange(0.0, 4*pi, 0.01)
def pltsin(f1, f2):
ax11 = plt.subplot(121)
ax11.set_title('Plot 1')
ax11.plot(t, sin(2*pi*t*f1/4/pi), 'k'); ax11.grid(True)
ax11.plot(t, cos(2*pi*t*f1/4/pi), 'r'); ax11.grid(True)
ax12 = plt.subplot(122)
ax12.set_title('Plot 2')
ax12.plot(t, sin(2*pi*t*f2/4/pi), 'k'); ax12.grid(True)
ax12.plot(t, cos(2*pi*t*f2/4/pi), 'r'); ax11.grid(True)
plt.show()
def reset_values(b):
"""Reset the interactive plots to inital values."""
my_plts.children[0].value = 1
my_plts.children[1].value = 1
reset_button = widgets.Button(description = "Reset")
reset_button.on_click(reset_values)
my_plts = widgets.interactive(pltsin, f1 = (1, 2, 0.01), f2 = (1, 2, 0.01))
display(my_plts, reset_button)
不能忍受硬编码的变量吗?然后将reset_values
函数替换为更具弹性的版本:
Can't stand hard-coded variables? Then replace the reset_values
function with this more elastic version:
def reset_values(b):
"""Reset the interactive plots to inital values."""
my_plts.children[0].value = my_plts.children[0].min
my_plts.children[1].value = my_plts.children[1].min
希望有帮助.