4: Visualization and Plotly 4.12: 3D Plotting

Unit 4.12: 3D Plotting

Three-Dimensional Plotting in Plotly

Plotly is a popular open-source Python library used for creating interactive, publication-quality visualizations. It is widely used in data science, analytics and machine learning for presenting data insights visually and interactively. It supports a wide variety of charts including line plots, scatter plots, bar charts, pie charts, heatmaps and 3D plots. It integrates well with Jupyter notebooks, Dash and web applications.

1
2
3
import numpy as np import pandas as pd import plotly.graph_objects as go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#function def f(x, y): return np.sin(np.sqrt(x ** 2 + y ** 2)) # Data x = np.linspace(-6, 6, 30) y = np.linspace(-6, 6, 30) X, Y = np.meshgrid(x, y) Z = f(X, Y) # Convert to a pandas DataFrame (long format) df = pd.DataFrame({ "X": X.ravel(), "Y": Y.ravel(), "Z": Z.ravel() }) df.head()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Plotly surface plot fig = go.Figure(data=[ go.Surface( x=X, y=Y, z=Z, colorscale='Viridis', showscale=True ) ]) fig.update_layout( title='3D Surface: sin(sqrt(x² + y²))', scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z') ) fig.show()
Drag to rotate · Scroll to zoom