11.2.8: Using ipywidgets for Interactivity
In order to produce interactive plots and figures, Python modules such as ipywidgets provide a set of widgets which you may embed into your Libretexts page.
First, we will import all of our dependencies. You may
import ipywidgets
directly, or you can pick specific widgets to import as done below. Even though the rest of the code is separated into different code blocks, each Libretexts page will "remember" all of the imports done on that page, so we only need to do this once at the beginning.
Next, lets define some functions to help us make an interactive matplotlib sine graph.
Next, we define our updating function. Generally, this is a function that will accept the widget values and then adjust the data of our plot accordingly. The function below will take the values from our amplitude and frequency widgets as its parameters and then create a new set of y values. Interact passes these values to the function through the sliders.
The easiest way to to display an interactive plot with ipywidgets is with the interact() function as shown above, though there are far more features available through ipywidgets that users are encouraged to explore on their own.