Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

3D Scatter Plots

Plot three numeric variables in 3D and understand when 3D helps (and when it doesn’t).

Use 3D carefully

3D plots can look impressive but are often harder to interpret than they seem — a screen is 2D, so any 3D chart is really a projection, and projections distort distance and depth judgment. Two points that look close together might be far apart along the axis pointing "into" the screen.

Use 3D when:

  • You genuinely need three continuous numeric variables plotted together
  • Interaction (rotate/zoom) helps understanding, and the reader will actually rotate it
  • Two 2D scatter plots (or a bubble chart) can't tell the same story

If you just want to add a third variable to a 2D scatter, prefer color or bubble size (see the previous two pages) — it's usually clearer than forcing a third spatial axis.

Example

Python
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "x": [1, 2, 3, 4, 5],
    "y": [10, 6, 8, 4, 7],
    "z": [100, 120, 90, 150, 110],
    "group": ["A", "A", "B", "B", "A"],
})
 
fig = px.scatter_3d(df, x="x", y="y", z="z", color="group", title="3D scatter")
fig.show()

In the rendered chart, dragging rotates the camera and scrolling zooms — that interactivity is what makes a 3D plot usable at all; a static screenshot of the same figure often hides more than it reveals.

sketch A 3D scatter, projected onto a 2D screen p5.js
Points sit at (x, y, z); depth (z) is faked here with an isometric-style projection and size/brightness falloff.

Tips

  • Rotate before you conclude anything — a single static angle can hide clusters or trends.
  • Prefer scatter_3d only when a 2D-plus-color/size chart genuinely loses information.
  • Heavy point counts get cluttered fast in 3D; consider sampling or aggregating first.
diagram Should this be a 3D scatter? mermaid
3D adds real value only when a 2D chart with color or size can't capture the pattern.

Check your understanding

quizCheck yourself
  1. Which function draws the 3D scatter plot in the lesson?

    Show answer

    A — px.scatter_3d — px.scatter_3d takes x, y and z columns, with color for an optional group.

  2. When does the lesson say 3D is justified?

    Show answer

    B — You genuinely need three continuous variables and rotating helps, and 2D charts cannot tell the same story — A screen is 2D, so a 3D chart is a projection that distorts distance. Prefer color or bubble size for a third variable when possible.

  3. Why should you rotate a 3D scatter before drawing conclusions?

    Show answer

    B — A single static angle can hide clusters behind other points — Any one camera angle can accidentally hide a cluster or trend, which is why interactivity matters.

  4. Your 3D chart has far too many points and is cluttered. What does the lesson suggest?

    Show answer

    D — Sample or aggregate first — Heavy point counts get cluttered fast in 3D, so sample or aggregate.

Next

Continue to: Creating Dashboards with Plotly to combine multiple charts into one view.

🧪 Try It Yourself

Exercise 1 – Build a 3D Scatter

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Color the Third Group

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – When to Avoid 3D

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did