How to create interactive Jupyter Notebook or JupyterLab 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
The result:
What if Notebook does not plot the data only creates the interactive part of the plot? With white or the 3D scale background?


Install Node.Js
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
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