If you want to try Plotly instead of Matplotlib, use the following code instead:
import ipywidgets as widgets
from IPython.display import display
import plotly.graph_objs as go
import plotly.offline as py
import plotly.express as px
import numpy as np
# generate x values
x = np.linspace(0, 2 * np.pi, 100)
def my_sine(x, w, amp, phi):
"""
Return a sine for x with angular frequeny w and amplitude amp.
"""
return amp*np.sin(w * (x-phi))
@widgets.interact(w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))
def view_image(w = 1.0, amp=1, phi=0):
y = my_sine(x, w, amp, phi)
fig = px.line(x=x, y=y)
#Fix Y-axis range, otherwise the sine will appear to remain fixed, and only Y-axis values will change.
fig.update_yaxes(range=(-4, 4))
#Disable autosizing fo Figure canvas. Otherwise, every time a slider is moved, the canvas size will automatically change.
fig.update_layout(autosize=False)
fig.show()
In order to install Plotly and use it in your notebooks, please read this guide.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…