Interactive Jupyter Notebook

How to create interactive Jupyter Notebook or JupyterLab plots.

Fixed view-angle 3D plot
Note1: related Notebook for testing: ipynb, html, pdf or GitHub (Notes2)
Note2: this post is a reflection to my 3D plotter GUI post

If a 3 dimensional (scatter) plot can be converted to a rotatable plot that enormously increases the understanding of the plotted dataset. When zooming / rescaling and saving options are added to the functions that is another positive advantage compared to simple plots achievable using "simple" matplotlib plots.

import matplotlib.pyplot as plt
%maptlotlib inline

The second code line is an iPython magic command, which is the first step in having plots at all in Notebooks. The inline flag allows notebook to display the generated plot in the Notebook, below the ending code.

# displaying the plot 
plt.show()

Note that in such situation matplotlib module creates static images of the 3D dataset at a specific angle that offers a one-angle viewpoint highly limiting proper understanding of the plotted data (see image above on the right).

Interactive plots in Notebooks

There are different ways to get an interactive plot. For example one of these code lines should replace the "%matplotlib inline"

%matplotlib notebook
%matplotlib widget %matplotlib inline

widget reference

The result:

3D plot - rotating 3D plot - zooming 

What if Notebook does not plot the data only creates the interactive part of the plot? With white or the 3D scale background?

Error - white background Error - 3D scale background 
These mean that matplotlib and related modules are not properly installed or enabled in the Notebook application. In my case JupyterLab and Jupyter Notebook are installed as a part of the Anaconda Python package.

Install Node.Js

If Node.Js is not installed yet... it is required to build the Notebook (JupyterLab) widgets extension package. Interactivity requires javascript functionality.
conda install nodejs

You could use -y flag which sets 'yes' for all questions. It is generally not recommended, because it means that either you are aware of possible consequences and then you don't do it, or you just do not care about what happens during installation, which is a dangerous attitude. :)

Upgrade JupyterLab

There may be several reasons for this faulty function, but the most probable is that the installed modules are outdated. It seems that Jupyter notebook version below 8.x can show this buggy behaviour.
conda install --upgrade jupyterlab

The above command run in Anaconda prompt window upgrades your local JupyterLab module.

It is important to note that on every modification the Jupyter server should be restarted, better to run updates/upgrades or additional module installation after shutting down the open Notebook application (and the running server in the background).

Install and allow Notebook extensions

Now you may fall on an error indicating that some Notebook extension ('nbextension') is not working properly. In this case run

conda install -c conda-forge jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

These install the nbextension package as a Notebook server extension first then the second line copies the installed javascript and css files and edits some Jupyter config files for proper functionality.

To enable the extensions run

jupyter nbextension enable --py widgetsnbextension

Adding Matplotlib to Jupyter services

If from the beginning there was no matplotlib module installed for the Jupyter services then

jupyter labextension install jupyter-matplotlib
jupyter labextension install @jupyter-widgets/jupyterlab-manager

commands will allow you to use matplotlib for plotting.

IPYMPL module requested

It may happen that running the plotting code block (cell) in the Notebook returns with an ipympl error. In such case run (as defined in conda install) the i-python-matplotlib installer

conda install conda-forge::ipympl

IPYWIDGETS module requested

install -U ipywidgets

-U flag stands for upgrade (only), in case there was no module preinstalled remove the flag. for complete install

The 3D bar plot code

original code source, modified to use ipympl extension

# creating 3d bar plot using matplotlib  
# in python 
  
# to interact with plot 
%matplotlib ipympl
  
# importing required libraries 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
  
# creating random dataset 
xs = [2, 3, 4, 5, 1, 6, 2, 1, 7, 2] 
ys = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
zs = np.zeros(10) 
dx = np.ones(10) 
dy = np.ones(10) 
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
  
# creating figure 
figg = plt.figure() 
ax = figg.add_subplot(111, projection='3d') 
  
# creating the plot 
plot_geeks = ax.bar3d(xs, ys, zs, dx,  
                      dy, dz, color='blue') 
  
# setting title and labels 
ax.set_title("3D bar plot") 
ax.set_xlabel('x-axis') 
ax.set_ylabel('y-axis') 
ax.set_zlabel('z-axis') 
  
# displaying the plot 
plt.show()
  

Related sites visited while the final solution was reached: https://stackoverflow.com/questions/49647705/jupyter-nbextensions-does-not-appear
https://stackoverflow.com/questions/65357800/i-am-using-matplotlib-widget-but-it-is-not-printing-plots-in-the-output
https://stackoverflow.com/questions/73715821/jupyter-lab-issue-displaying-widgets-javascript-error
https://stackoverflow.com/questions/49647705/jupyter-nbextensions-does-not-appear
https://github.com/jupyterlab/jupyterlab/issues/14270

Animated gifs created with
https://ezgif.com/video-to-gif
https://cloudconvert.com/mp4-to-gif

No comments:

Post a Comment

Snowflake universe, part #6 - Forecasting2

Forecasting with built-in ML module Further posts in  Snowflake  topic SnowFlake universe, part#1 SnowFlake, part#2 SnowPark Notebook Snow...