Make 3D interactive Matplotlib plot in Jupyter Notebook
Matplotlib is a data visualization library. It is difficult to analyze/get an insight into the data without visualizing it. And matplotlib is a great library for doing the visual analysis of data in python. Using matplotlib we can plot 1-D, 2-D and even 3-D data. In this article, we are going to learn how we can plot various 3-D plots using the matplotlib. To plot 3-D plots in python, we need to import the mplot3d library from the standard installation of matplotlib library from python.
As matplotlib is a third-party library, it doesn’t come with a standard installation of python, so you need to install matplotlib before following this article. Below is the command using which you can install the matplotlib library.
pip3 install matplotlib
To make the plots interactive all you need to do is install another library called ipympl i.e. interactive python matplotlib.
pip3 install ipympl
For creating 3d figure Axes3D.plot() function is used.
Axes3D.plot(xs, ys, zs,*args, **kwargs)
- xs: the x coordinate value of the vertices.
- ys: the y coordinate values of the vertices.
- zs: The z coordinate value(s), either one for all points or one for each point.
To generate an interactive 3D plot first import the necessary packages and create a random dataset. Now using Axes3D(figure) function from the mplot3d library we can generate a required plot directly. Pass the data to the 3D plot and configure the title and labels.
Note: We must needed to add ” %matplotlib widget “, it is a Jupyter magic widget and used to tell jupyter to use interactive backend for plot. We have to add it to the top of the script to create an interactive plot in the ipython notebook (i.e. Jupyter notebook, Google Colab, Kaggle Kernel, etc.) to renders the figure as an interactive figure.
Given below are some implementations to do the same.
Example: Interactive scatter plot
Example: Interactive bar plot