How to install and enable the ipywidgets module

Before getting started, we will have to install ipywidgets. We will use the pip install ipywidgets command to do this. After this has been successfully installed, we need to enable the extension within the Jupyter Notebook. To do that, use the jupyter nb extension enable - -py widgetsnbextension command, which will enable the widgets to interact with the Jupyter Notebook.

Begin as usual by importing and setting up the Matplotlib environment, including the ipywidgets module, the interactive, fixed, and interact_manual methods, as well as ipywidgets itself as widgets:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

To start with, we'll take a look at a simple function that displays a straight line. The 'f' function of x displays a range of numbers between 0 and 10 with a linear function that has a slope of X. Therefore, f (10), as shown in the following code, gives a linear function with a slope of 10:

# Basic interact usage with integer slider
def f(x):
plt.plot(np.arange(0,10), x*np.arange(0,10))
plt.ylim(-30,30)
f(10)

The following is the output of the preceding code:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.222.168