I was working on a data visualization project where I needed to display relationships between three variables, sales, profit, and customer satisfaction, across different U.S. states.
While 2D plots are great for many use cases, I wanted to add a third dimension to the visualization to better understand how these variables interact. That’s when I decided to create a 3D scatter plot using Python’s Matplotlib and NumPy.
In this tutorial, I’ll show you how to easily create and customize a 3D scatter plot from a NumPy array in Python. I’ll also share some practical examples and customization techniques that I use in my own projects.
What is a 3D Scatter Plot in Python Matplotlib?
A 3D scatter plot is a type of plot that displays points in three-dimensional space. Each data point has three coordinates, X, Y, and Z, which makes it perfect for visualizing relationships between three numerical variables.
In Python, the Matplotlib library (especially the mpl_toolkits.mplot3d module) makes it incredibly easy to create 3D visualizations. Combined with NumPy, you can generate, manipulate, and visualize large datasets efficiently.
Method 1 – Create a Basic 3D Scatter Plot Using NumPy and Matplotlib
Let’s start with a simple example. I’ll generate three NumPy arrays (X, Y, and Z) representing random data points and then plot them in 3D using Matplotlib.
Here’s the full Python code:
# Import necessary Python libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate random data using NumPy
np.random.seed(42)
x = np.random.rand(100) * 100 # X-axis data (e.g., Sales)
y = np.random.rand(100) * 50 # Y-axis data (e.g., Profit)
z = np.random.rand(100) * 75 # Z-axis data (e.g., Customer Satisfaction)
# Create a 3D figure in Matplotlib
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Create a 3D scatter plot
ax.scatter(x, y, z, color='dodgerblue', s=60, alpha=0.8, edgecolors='k')
# Add labels and title
ax.set_xlabel('Sales (in USD)')
ax.set_ylabel('Profit (in USD)')
ax.set_zlabel('Customer Satisfaction (%)')
ax.set_title('3D Scatter Plot of Sales, Profit, and Satisfaction')
# Show the plot
plt.show()I executed the above example code and added the screenshot below.

This code creates a clean and interactive 3D scatter plot. You can rotate the plot, zoom in, and explore the relationships between your variables visually.
Method 2 – Add Color Based on a Fourth Variable
In many real-world Python projects, you may want to represent a fourth dimension, such as region, category, or performance level, using color.
Let’s modify the previous example to color the points based on another NumPy array.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate random data
np.random.seed(10)
x = np.random.rand(100) * 100
y = np.random.rand(100) * 80
z = np.random.rand(100) * 60
color_data = np.random.rand(100) # Fourth variable for coloring
# Create the 3D scatter plot
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Use color mapping to represent the fourth variable
scatter = ax.scatter(x, y, z, c=color_data, cmap='viridis', s=70, alpha=0.9)
# Add color bar
cbar = plt.colorbar(scatter)
cbar.set_label('Performance Index')
# Add axis labels and title
ax.set_xlabel('Sales (in USD)')
ax.set_ylabel('Profit (in USD)')
ax.set_zlabel('Customer Satisfaction (%)')
ax.set_title('3D Scatter Plot with Color Mapping in Python')
plt.show()I executed the above example code and added the screenshot below.

This version adds a color gradient to your plot, which makes it easier to identify trends and clusters. I often use this approach when analyzing business or environmental data where multiple variables are related.
Method 3 – 3D Scatter Plot from a NumPy Array Matrix
Sometimes, your data is already stored in a NumPy 2D array or matrix (e.g., from a CSV file or a simulation).
Let’s see how you can use a single NumPy array to create a 3D scatter plot.
import numpy as np
import matplotlib.pyplot as plt
# Create a NumPy 2D array with three columns (X, Y, Z)
data = np.random.rand(150, 3) * [100, 80, 60]
# Extract columns for plotting
x, y, z = data[:, 0], data[:, 1], data[:, 2]
# Create the 3D scatter plot
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, color='crimson', s=50, alpha=0.8)
# Add labels and title
ax.set_xlabel('Advertising Spend ($)')
ax.set_ylabel('Revenue ($)')
ax.set_zlabel('Customer Retention (%)')
ax.set_title('3D Scatter Plot from NumPy Array Matrix in Python')
plt.show()I executed the above example code and added the screenshot below.

This method is particularly useful when your dataset is already in a structured NumPy format. It’s also easier to integrate with data pipelines or machine learning workflows where NumPy arrays are common.
Method 4 – Customize Your 3D Scatter Plot
Python’s Matplotlib offers plenty of customization options to make your plots visually appealing and informative.
Here’s how you can adjust marker size, color, and viewing angles.
import numpy as np
import matplotlib.pyplot as plt
# Generate random data
x = np.random.rand(120) * 100
y = np.random.rand(120) * 70
z = np.random.rand(120) * 90
# Create figure and 3D axes
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Customize markers and colors
ax.scatter(x, y, z, c='orange', s=80, marker='^', edgecolors='black', alpha=0.9)
# Customize viewing angle
ax.view_init(elev=25, azim=45)
# Add grid and labels
ax.grid(True)
ax.set_xlabel('Marketing Budget ($)')
ax.set_ylabel('Leads Generated')
ax.set_zlabel('Conversion Rate (%)')
ax.set_title('Customized 3D Scatter Plot in Python Matplotlib')
plt.show()With just a few lines of code, you can completely change the look and feel of your 3D scatter plot. I use these customizations when preparing visual reports for business teams or clients.
Method 5 – 3D Scatter Plot with Multiple Data Groups
If you want to compare multiple categories, say, different regions in the U.S., you can plot multiple datasets in the same 3D space.
Here’s how I usually do it:
import numpy as np
import matplotlib.pyplot as plt
# Generate data for three regions
np.random.seed(0)
east = np.random.rand(50, 3) * [100, 80, 60]
west = np.random.rand(50, 3) * [90, 70, 50]
south = np.random.rand(50, 3) * [110, 85, 65]
# Create the figure
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Plot each region with different colors
ax.scatter(east[:,0], east[:,1], east[:,2], color='blue', label='East', s=60)
ax.scatter(west[:,0], west[:,1], west[:,2], color='green', label='West', s=60)
ax.scatter(south[:,0], south[:,1], south[:,2], color='red', label='South', s=60)
# Add labels, legend, and title
ax.set_xlabel('Revenue ($)')
ax.set_ylabel('Expenses ($)')
ax.set_zlabel('Profit Margin (%)')
ax.set_title('3D Scatter Plot by Region in Python')
ax.legend()
plt.show()This approach is perfect for comparing datasets side by side. It helps you quickly identify which group performs better across multiple dimensions.
Tips for Working with 3D Scatter Plots in Python
- Keep your dataset clean and scaled properly for accurate visualization.
- Use color maps (cmap) to represent additional variables intuitively.
- Avoid clutter — too many points can make 3D plots hard to read.
- Experiment with different view angles using ax.view_init().
- Combine 3D scatter plots with 2D projections for deeper insights.
Creating a 3D scatter plot from a NumPy array in Python Matplotlib is one of the most powerful ways to visualize multi-dimensional data.
Whether you’re analyzing business performance, environmental data, or scientific simulations, 3D plots help you uncover patterns that 2D charts can’t show.
I hope you found this tutorial helpful. Try experimenting with your own datasets and see how these visualizations can bring your data to life.
You may also like to read:
- Zooming in 3D Scatter Plots in Python Matplotlib
- Save a 3D Scatter Plot in Python using Matplotlib
- Create a Matplotlib 3D Scatter Animation in Python
- Set Xlim and Zlim in Matplotlib 3D Scatter Plot

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.