An example of using version 4.x widgets in a Jupyter Notebook. Reference: https://ipywidgets.readthedocs.io/en/latest/.
Depending on the current state of your Python installation, you may have to install the ipywidgets package, have notebook>=4.2.0, and enable widgets.
$ pip install ipywidgets
$ jupyter nbextension enable --py --sys-prefix widgetsnbextension
All of the imports:
In [ ]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact
Define a sine wave:
In [ ]:
x = np.linspace(0, 1, 101)
k = 2
f = np.sin(2*np.pi * k * x)
Plot the sine wave:
In [ ]:
plt.plot(x, f)
Define a function that allows a user to vary the wavenumber of the sine wave:
In [ ]:
def sine_plotter(wave_number):
plt.plot(x, np.sin(2*np.pi * x * wave_number), 'r')
For example:
In [ ]:
sine_plotter(5)
Use a slider widget to call sine_plotter and interactively change the wave number of the sine wave:
In [ ]:
interact(sine_plotter, wave_number=(1, 10, 0.5))
Neato!