3D Scatter Plot in Plotly

Last Updated: 08 Nov 2025


3D Scatter plots show three variables in 3D space — perfect for multivariate data, scientific visualization, machine learning clusters, or 3-axis relationships.

Hinglish Tip: 3D Scatter = "teen dimensions ek saath" — x, y, z! Rotate karo, zoom karo, data ka 360° view lo.


Basic 3D Scatter

import plotly.express as px

# Sample data
df = px.data.iris()

fig = px.scatter_3d(
    df,
    x='sepal_length',
    y='sepal_width',
    z='petal_length',
    color='species',
    title="Iris Dataset in 3D"
)
fig.show()

Key Parameters in px.scatter_3d()

ParameterValues / ExamplesPurpose
x, y, zColumn names3 axes data
colorColumnColor by category
sizeColumnBubble size (4th dimension!)
symbolColumnDifferent marker shapes
hover_nameColumnBold label on hover
hover_data['petal_width']Extra info
titleStringChart title
labels'z': 'Petal Length'Rename axes
opacity0.8See through overlapping points

Full Example: Student Performance (Marks, Attendance, Study Hours)

import plotly.express as px
import pandas as pd
import numpy as np

# Generate sample student data
np.random.seed(42)
n = 100
df = pd.DataFrame({
    'Math': np.random.normal(75, 15, n),
    'Science': np.random.normal(78, 12, n),
    'Attendance (%)': np.random.uniform(60, 100, n),
    'Study Hours': np.random.uniform(1, 8, n),
    'Grade': np.random.choice(['A', 'B', 'C', 'D'], n, p=[0.3, 0.4, 0.2, 0.1])
})

fig = px.scatter_3d(
    df,
    x='Math',
    y='Science',
    z='Attendance (%)',
    color='Grade',
    size='Study Hours',
    symbol='Grade',
    hover_name='Grade',
    hover_data=['Study Hours'],
    opacity=0.8,
    title='Student Performance in 3D (Size = Study Hours)',
    labels={
        'Math': 'Math Marks',
        'Science': 'Science Marks',
        'Attendance (%)': 'Attendance'
    }
)

fig.update_layout(
    scene=dict(
        xaxis_title='Math',
        yaxis_title='Science',
        zaxis_title='Attendance %',
        camera=dict(eye=dict(x=1.5, y=1.5, z=1.5))
    ),
    legend_title="Grade",
    font=dict(size=12)
)

fig.show()

Hinglish Tip:size='Study Hours' → bada point = zyada padhai — teacher ko turant pata chalega!


Add Trend Surface (Regression Plane)

fig = px.scatter_3d(df, x='Math', y='Science', z='Attendance (%)', color='Grade')

fig.update_traces(marker=dict(size=5))

# Add regression surface (using graph objects)
import plotly.graph_objects as go
from sklearn.linear_model import LinearRegression

X = df[['Math', 'Science']]
y = df['Attendance (%)']
model = LinearRegression().fit(X, y)
x_surf, y_surf = np.meshgrid(np.linspace(df['Math'].min(), df['Math'].max(), 10),
                             np.linspace(df['Science'].min(), df['Science'].max(), 10))
z_surf = model.predict(np.c_[x_surf.ravel(), y_surf.ravel()]).reshape(x_surf.shape)

fig.add_trace(go.Surface(x=x_surf, y=y_surf, z=z_surf, opacity=0.5, showscale=False))

fig.update_layout(title="3D Scatter with Trend Plane")
fig.show()

Common Mistakes to Avoid

MistakeFix
Points overlap → can't seeUse opacity=0.7 + size wisely
Bad camera angleUse camera=dict(eye=dict(x=1.5,y=1.5,z=1.5))
No color/size meaningAlways use color + size for 4th/5th variable
Too many points → slowSample data: df.sample(500)
Can't exportInstall kaleido