Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Create a 3D Scatter Plot from a NumPy Array in Matplotlib

Create a 3D Matplotlib scatter plot from an N-by-3 NumPy array by mapping its columns to x, y, and z coordinates.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To plot an (N, 3) NumPy array in 3D, create a Matplotlib axes with projection="3d" and pass its three columns to ax.scatter(): the first column is x, the second y, and the third z.

Plot an N-by-3 array in 3D

Each row below represents one point, with its x, y, and z coordinates in that order. The 3D scatter method belongs to the projected axes, so call ax.scatter() rather than the ordinary 2D plt.scatter() function.

import matplotlib.pyplot as plt
import numpy as np

# One point per row; columns are x, y, and z.
points = np.array([
    [0.0, 1.0, 2.0],
    [1.0, 0.5, 3.0],
    [2.0, 2.0, 1.0],
])

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.scatter(points[:, 0], points[:, 1], points[:, 2])
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()

This follows Matplotlib’s documented 3D scatter plot example: set up a 3D subplot, then pass the coordinate sequences to its scatter method.

How the columns map to coordinates

For an array with shape (N, 3), N is the number of points and each of the three columns contains one coordinate for every point:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • points[:, 0] supplies x coordinates.
  • points[:, 1] supplies y coordinates.
  • points[:, 2] supplies z coordinates.

The slices select every row in one column. Keep the coordinate arrays aligned: x, y, and z should each contain one value per plotted point. The Axes3D.scatter API accepts array-like x and y positions and either an array-like z sequence or a scalar z value. A scalar places all points at that same height, which is useful for a flat plane but not for plotting three distinct columns.

Use an alternative 3D axes setup

You can create the same kind of axes with plt.subplots() by specifying its projection in subplot_kw:

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.scatter(points[:, 0], points[:, 1], points[:, 2])
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()

Both approaches create a Matplotlib Axes3D object. The mplot3d toolkit documentation explains how the projection="3d" setting selects it.

Change marker size or color

Pass optional arguments to ax.scatter() to adjust the points. For example, use a numeric array as c to color points according to a fourth measurement, such as a value stored separately from the three coordinates:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
values = np.array([10, 20, 30])
ax.scatter(
    points[:, 0],
    points[:, 1],
    points[:, 2],
    s=40,
    c=values,
    cmap="viridis",
)

Here s sets marker area in points squared, while numeric c values are mapped through the selected colormap. The API also allows a single color or per-point colors. See the scatter parameter reference for the available options, including depthshade, which controls depth shading.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Rotate the plot and understand its limits

Matplotlib’s mplot3d displays a projection of a 3D scene. In interactive backends, you can drag the plot to rotate the view and use the mouse to zoom. It is a convenient choice when you want a 3D plot within Matplotlib, but Matplotlib’s mplot3d overview cautions that it is not the fastest or most feature-complete 3D library.

The axlim_clip option for hiding points outside the axes view limits was added in Matplotlib 3.10. Use it only when running that version or newer; consult the API documentation for its exact behavior.

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.

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

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.