October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Create a 3D Scatter Plot in Python Matplotlib

A practical Matplotlib recipe for plotting three numeric coordinates, styling points, and judging when a 3D view is useful.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create a 3D scatter plot by making a Matplotlib axes with projection="3d", passing your x, y, and z coordinates to ax.scatter(), and labeling each axis. The example below uses repeatable sample data; replace it with your own three numeric variables.

Make a basic 3D scatter plot

Install Matplotlib and NumPy if they are not already available in your Python environment. Then run:

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt
import numpy as np

# Repeatable illustrative data, not a real dataset.
rng = np.random.default_rng(42)
n = 100
x = rng.uniform(0, 10, n)
y = rng.uniform(0, 10, n)
z = rng.uniform(0, 10, n)

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.scatter(x, y, z)

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.show()

The seed makes this example’s sample values reproducible; it does not make the random points meaningful measurements. For a more compact figure-and-axes setup, Matplotlib also supports fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) before calling ax.scatter(x, y, z). Both approaches create a 3D axes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How the coordinates become points

ax = fig.add_subplot(projection="3d") creates the 3D axes, and ax.scatter(xs, ys, zs) plots the coordinates. Values correspond by position: the first x, y, and z values make one point, the second values make another, and so on. Supply coordinate arrays with matching lengths for the usual case. The API also permits zs to be a single scalar, which places all points at the same z position; its default is 0. See the Axes3D.scatter API reference and the official 3D scatter gallery example.

For example, ax.scatter(x, y, z, zdir="y", zs=5) places the supplied two-dimensional data on the x-z plane, with the shared position of 5 along y. This is useful when plotting 2D coordinates on a plane inside the 3D axes; it is not the same as supplying a separate y-coordinate for every point.

Encode an additional variable with color or size

Scatter options can communicate more than the three spatial coordinates. The s parameter sets marker area in points squared and accepts either one value or per-point values. The c parameter accepts a color or per-point colors; numeric values can be mapped through a colormap and normalization. For example, using z for color adds a second visual cue for the same measurement:

points = ax.scatter(x, y, z, c=z, cmap="viridis", s=30)
fig.colorbar(points, ax=ax, label="Z value")

The colorbar explains what the colors mean. If color represents a different variable, label the colorbar with that variable instead. For categories, use distinct colors or marker shapes and include a clear legend. Avoid adding encodings that make the plot harder to read.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The depthshade option controls shading intended to suggest depth. Matplotlib applies it independently to each scatter call, so inspect the combined appearance when plotting multiple separately colored groups rather than assuming shading is applied globally. Details and defaults are in the scatter API reference.

Use version-specific scatter options carefully

The stable API reference identifies axlim_clip as added in Matplotlib 3.10; it hides points outside the axes’ view limits. It identifies depthshade_minalpha, which controls the minimum alpha used for depth shading, as added in Matplotlib 3.11. These options are unavailable in older versions, so check your installed Matplotlib version before using them. Stable documentation can change as releases advance.

Rotate the plot, then check whether 3D helps

Matplotlib’s mplot3d toolkit draws a 3D scene as a 2D projection. This can cause points to overlap, and viewing angle or perspective can make relationships and apparent distances difficult to interpret. Rotate the view and verify that the axis labels and scales make the plotted dimensions clear. If precise comparisons matter, consider pairwise 2D scatter plots, where each coordinate relationship is shown without a 3D projection.

With an interactive backend, you can rotate and zoom using mouse gestures. Toolbar pan and zoom buttons do not operate in the same way for 3D plots as they do for 2D plots; see Matplotlib’s interactivity guidance. The mplot3d toolkit documentation describes it as a simple plotting toolkit included with Matplotlib and notes that it is less mature than Matplotlib’s 2D plotting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do you need to import Axes3D?

Not for the modern setup shown here: fig.add_subplot(projection="3d") works without an explicit from mpl_toolkits.mplot3d import Axes3D import. Matplotlib’s guide says that explicit import ceased to be necessary in version 3.2.0, which explains why older tutorials may still include it. The mplot3d tutorial shows how to create 3D axes.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.