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 desk2 min

How to Create a 3D Scatter Plot with Color in Python Matplotlib

Use Matplotlib’s 3D axes to plot x, y, and z coordinates, then choose a colormap and colorbar for numeric values or explicit colors and a legend for categories.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Matplotlib’s 3D axes and pass a numeric array to c to color each point by a value. Pair that mapping with a colormap and colorbar so readers can interpret the colors. For categories, use explicit group colors and a legend instead.

Create a 3D scatter plot colored by a numeric value

Each observation needs matching x, y, z coordinates and a color value. This example uses one value per point and labels the color scale.

import matplotlib.pyplot as plt
import numpy as np

# Each array contains one entry per observation.
x = np.array([1, 2, 3, 4])
y = np.array([2, 1, 4, 3])
z = np.array([0.5, 1.2, 0.7, 1.8])
values = np.array([10, 25, 40, 60])

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
points = ax.scatter(x, y, z, c=values, cmap="viridis")
fig.colorbar(points, ax=ax, label="Measured value")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()

The 3D axes are created with projection="3d", and ax.scatter(x, y, z, ...) plots the coordinates. Matplotlib maps the numeric c values through the selected cmap; the colorbar, linked to the returned scatter object, explains that mapping. Replace “Measured value” with the quantity and units represented by your data. The official 3D scatterplot example shows the basic 3D scatter workflow.

Choose colors that match what the data means

Continuous numeric values

Use one numeric value per point in c, choose a colormap suited to the quantity, and add a labeled colorbar. Matplotlib also accepts norm to control how values are scaled into the colormap. See the Axes3D.scatter API for the supported color inputs and mapping options.

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.

Discrete categories

For groups such as “A,” “B,” and “C,” assign deliberate colors to each category—either by supplying explicit colors per point or by plotting each group separately with a fixed color. Identify the groups with a legend. A continuous-looking colorbar suggests a numeric scale, so it is not a suitable key for unordered categories.

One uniform color

If all points should have the same color, pass a named color or color format rather than a numeric array. This avoids implying that color varies with a data value.

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

Check the data and rendering

  • Make sure x, y, z, and per-point c values have the same number of entries and refer to the same observations.
  • Use a colorbar for a continuous numeric mapping and a legend for category colors.
  • depthshade changes marker rendering to suggest depth; it is enabled by default in the current API documentation. It is separate from the data-to-color mapping, so it should not be used as a substitute for a colormap or key.

Matplotlib describes mplot3d as a simple 3D plotting toolkit and notes that 3D plotting is less mature than 2D. Interactive backends can allow rotation and zooming, which may help inspect points from different angles. See the mplot3d toolkit documentation.

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.

Free tools Windows power users keep installed

One-click scans. No signup required.

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
Crashes, No Sound, or Screen Glitches?Free driver scan

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.