To display easy way to plot 2D and 3D interactive plots with Plotly in the Jupyter notebook
In one of the previous posts, I illustrated how to easily plot 2D and 3D interactive plots with Matplotlib and its 3D in the Jupyter notebook. If you are interested in Matplotlib, please read that post title Create Interactive 2D and 3D Plots with Matplotlib in the Jupyter Notebook. In this post, we will learn how to easily plot 2D and 3D interactive plots with Plotly in the Jupyter notebook.
1. Python Packages for Interactive Plots
Besides Matplotlib and Plotly, Python has several other libraries to create interactive plots, such as:
2. Install Plotly
If you have not installed Plotly, you can directly install it in a Jupyter notebook using the following command.
!pip install plotly
3. 2D Interactive Plot
In this following example, let’s plot a 2D interactive scatter plot. We import required packages first, and then create a Pandas x, y DataFrame. You can import your own data if you want.
import plotly.express as px
import pandas as pd
df = pd.DataFrame(dict(
x = [0,2,4,3,5,7,6,9,8,10,11],
y = [0.1,2.5,3.5,3.8,4.6,7.5,5.6,8.5,10.5,9.5,11.3]
fig = px.scatter(df, x=”x”, y=”y”, title=”Simple scatter”,width=500, height=400)
The output looks as follows:
4. 3D Interactive Plot
Now let’s use Plotly to create 3D interactive plot. From the following and above code snippets, we can see that it is much easier with less code to Plotly to make a plot than to use Matplotlib.
import plotly.graph_objects as go
import numpy as np
x = np.linspace(0,4*np.pi,100)
y = np.sin(x)
z = 15 * np.random.random(100)
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
marker = dict(size = 5,
color = ‘blue’),)]) # model=’lines’
It renders the following outcome:
5. Online Course
If you are interested in learning Jupyter notebook in details, you are welcome to enrol one of my course Practical Jupyter Notebook from Beginner to Expert.