Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 42 ยท Maps and 3D

3D Scatter and Line Plots

Build interactive 3D scatter and line plots with Plotly: scatter_3d, line_3d, Scatter3d, colors, sizes, scene axes, camera and when 3D is the wrong choice.

Plotly can draw real, rotatable 3D charts in the browser using WebGL. You drag to rotate, scroll to zoom and hover to read values, with no extra library. 3D is the right tool when the data truly has three spatial or numeric dimensions: positions from a sensor, a trajectory, three measurements of the same object, clusters in a three-feature space. It is the wrong tool for ordinary comparisons, because perspective makes values hard to read (we return to that at the end).

This lesson covers the two basic 3D traces: scatter (points) and line (a path through space). The next lesson, 3D surfaces, meshes and cameras, covers continuous shapes.

Scatter in three dimensions

With Plotly Express, a 3D scatter plot is px.scatter_3d(df, x=, y=, z=). All the usual encodings work: color, size, symbol, hover_name, hover_data and opacity.

Python px.scatter_3d on the iris data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the layout, 3D charts do not use xaxis/yaxis. They use a scene: layout.scene holds xaxis, yaxis, zaxis, camera and aspectratio. A figure can have several scenes (scene, scene2, ...) which is how you place 3D charts in subplot cells of type "scene" (see make_subplots).

graph_objects: Scatter3d

go.Scatter3d takes x, y, z and a mode of "markers", "lines" or "markers+lines". Marker size is in pixels and does not shrink with distance, so keep it small for dense clouds. A numeric array for marker.color with a colorscale and showscale=True encodes a fourth variable.

A helix: Scatter3d in Plotly.js

Drag to rotate, scroll to zoom, double-click to reset the camera. If your browser or graphics driver has no WebGL, Plotly shows an error instead of a chart; every other chart type in this course still works.

Lines and trajectories

px.line_3d(df, x, y, z, color=...) draws a path through the points in row order, one line per color group. Sort your data first, because Plotly connects points in the order it receives them. Use it for flight paths, robot arms, particle tracks or a parametric curve.

Python Two trajectories with px.line_3d

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Controlling the scene

The scene layout is where most of the polish happens.

Property What it does
scene.aspectmode "auto" (default), "cube" (equal lengths), "data" (true proportions) or "manual"
scene.aspectratio With "manual": dict(x=1, y=1, z=0.5) stretches the box
scene.camera.eye Where the camera sits; larger values are farther away
scene.camera.projection.type "perspective" or "orthographic" (better for reading values)
scene.xaxis.range Fixes the axis range so the box does not jump
scene.xaxis.backgroundcolor Wall color; set showbackground=False for a clean look

Use aspectmode="data" when x, y and z have the same units (meters, for example). Without it, Plotly may stretch an axis and distort the shape of a trajectory.

Python
fig.update_layout(scene=dict(
    aspectmode="data",
    camera=dict(eye=dict(x=1.5, y=1.5, z=0.8), projection=dict(type="orthographic")),
    xaxis=dict(range=[-2, 2]), yaxis=dict(range=[-2, 2]),
))

When 3D is the wrong choice

Three dimensions look impressive, but they cost readability: points hide behind each other, perspective shrinks distant values, and a printed or static image shows only one viewpoint. Before using 3D ask whether you can show the same data with a 2D chart plus color or size (a bubble chart), a pair plot, or facets. Use 3D when the user needs to rotate to understand the structure, such as clusters, surfaces or real-world positions.

For large clouds (above about 100,000 points) 3D scatter stays smooth because it is drawn with WebGL, but give marker.size a small value and opacity below 1 so dense areas become visible instead of a solid blob.

flowchart LR
    A["Data with x, y, z"] --> B{"Points or path?"}
    B -->|Points| C["scatter_3d / Scatter3d"]
    B -->|Path in order| D["line_3d / mode lines"]
    B -->|Continuous grid| E["Surface (next lesson)"]
    C --> F["layout.scene: axes, camera, aspect"]
    D --> F
    E --> F

All 3D traces draw into a scene, which you style through layout.scene.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where are the 3D axes configured in a Plotly figure?

    3D traces draw into a scene. The scene holds its own xaxis, yaxis, zaxis, camera and aspect settings.
  2. 2. You have a robot arm's positions in time order. Which function draws the path?

    line_3d connects the points in the order they appear in the data, so sort them first.
  3. 3. Which setting keeps real-world proportions when x, y and z are all in meters?

    aspectmode="data" scales the box by the data ranges, so a trajectory is not stretched.
  4. 4. Which is a good reason to avoid 3D for a simple monthly sales comparison?

    Three dimensions add rotation and occlusion without adding information when the data has only two.

Summary

  • px.scatter_3d and px.line_3d draw points and paths; go.Scatter3d is the low-level version.
  • 3D traces live in layout.scene, which has its own axes, camera and aspect settings.
  • Sort data before drawing lines; use aspectmode="data" for equal units.
  • Keep markers small and semi-transparent for dense clouds.
  • Choose 3D only when rotation reveals structure that 2D cannot show.

Next: 3D Surfaces, Meshes and Cameras