这可以通过利用交互的 https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html#interactive功能。
%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
这个更有弹性的版本的功能:
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
希望有帮助。