Generating interactive displays in the Jupyter Notebook
In many situations, information changes dynamically, and it is desirable to present the changes graphically. In this recipe, we will discuss the creation of interacts in the Jupyter Notebook, which can be used to generate dynamic displays based on function evaluations. As an example, we will demonstrate how to create an interactive demonstration of the beats phenomenon from acoustics.
Getting ready
Start Jupyter and run the following three commands in an execution cell:
%matplotlib inline import numpy as np import matplotlib.pyplot as plt
How to do it…
We start by defining a function that plots a superposition of waves, with the following code:
xvalues = np.linspace(0, 40, 500) def plot_function(ratio=0.1): plt.figure(figsize=(10,3)) yvalues = np.cos(2*np.pi*xvalues) + \ np.cos(2*np.pi*ratio*xvalues) plt.plot(xvalues, yvalues, color='green', lw=2) plt.axis([0, 40, -2.1, 2.1]) plt.show()
Calling...