I was working on a Python project where I needed to visualize a dataset that had three continuous variables, something like sales, profit, and region density across different states in the USA.
While 2D scatter plots are great, I wanted to explore the data in a more interactive and visually appealing way. That’s when I turned to Matplotlib’s 3D scatter plots.
I’ve used Matplotlib extensively for data visualization in Python, and one of my favorite features is how flexible it is when it comes to customizing colors and styles. In this tutorial, I’ll walk you through how to create a 3D scatter plot with color in Python using Matplotlib, step by step.
What is a 3D Scatter Plot in Python?
A 3D scatter plot is a visualization that displays data points in three dimensions, X, Y, and Z. It’s especially useful when you want to explore relationships between three numerical variables.
In Python, the Matplotlib library provides an easy way to create 3D scatter plots using the Axes3D toolkit. You can also use color to represent a fourth dimension, such as intensity, density, or category.
Method 1: Create a Simple 3D Scatter Plot in Python
Let’s start with a basic 3D scatter plot. This will help you understand the structure before we add color customization.
Here’s the Python code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate random data
np.random.seed(42)
x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)
# Create a 3D figure
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# Plot the scatter
ax.scatter(x, y, z, color='blue', s=60)
# Add labels
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Basic 3D Scatter Plot in Python')
plt.show()You can refer to the screenshot below to see the output.

This simple code creates a 3D scatter plot with blue markers. It’s a great starting point if you’re just getting familiar with 3D visualization in Python.
Method 2: Add Color to Represent a Fourth Dimension
Now, let’s make things more interesting. We’ll use color to represent another variable, for example, sales growth across different regions.
Here’s the code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate random data
np.random.seed(10)
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
color = x + y + z # Fourth dimension
# Create 3D scatter with color mapping
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(x, y, z, c=color, cmap='viridis', s=70)
# Add color bar
fig.colorbar(scatter, ax=ax, shrink=0.6, label='Intensity')
# Add labels and title
ax.set_xlabel('Revenue')
ax.set_ylabel('Profit')
ax.set_zlabel('Market Share')
ax.set_title('3D Scatter Plot with Color in Python (Matplotlib)')
plt.show()You can refer to the screenshot below to see the output.

In this example, the color of each point corresponds to the value of x + y + z. The cmap=’viridis’ parameter defines the color palette. You can try other color maps like ‘plasma’, ‘coolwarm’, or ‘inferno’.
This visualization makes it easier to identify patterns, for example, which data points have higher combined values.
Method 3: Customize Marker Size and Color in Python
Sometimes, you may want to use marker size and color together to represent multiple dimensions. This is especially useful in business analytics or scientific visualization.
Here’s how you can do it:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate random data
np.random.seed(25)
x = np.random.rand(150)
y = np.random.rand(150)
z = np.random.rand(150)
color = np.random.rand(150)
size = 1000 * np.random.rand(150)
# Create 3D scatter with custom size and color
fig = plt.figure(figsize=(9, 7))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(x, y, z, c=color, cmap='coolwarm', s=size, alpha=0.7, edgecolors='k')
# Add color bar
fig.colorbar(scatter, ax=ax, shrink=0.5, label='Color Scale')
# Add labels
ax.set_xlabel('Customer Age')
ax.set_ylabel('Annual Income')
ax.set_zlabel('Spending Score')
ax.set_title('3D Scatter Plot with Custom Colors and Sizes in Python')
plt.show()You can refer to the screenshot below to see the output.

In this Python example, I used both size and color to represent different aspects of the data. The alpha parameter adds transparency, and edgecolors=’k’ gives each point a black border for better visibility.
This method is perfect when you want to add depth to your 3D scatter plot and make it more visually engaging.
Method 4: Use a Colormap Based on a Real Dataset
Let’s say you have real-world data, for example, average temperatures, humidity, and elevation for different cities in the USA. You can use color to represent temperature.
Here’s how you can do that in Python:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Example dataset (Temperature, Humidity, Elevation)
city = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
temperature = np.array([55, 65, 50, 70, 80])
humidity = np.array([60, 50, 65, 75, 40])
elevation = np.array([33, 305, 181, 43, 331])
color = temperature # Color based on temperature
# Create 3D scatter plot
fig = plt.figure(figsize=(9, 7))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(temperature, humidity, elevation, c=color, cmap='plasma', s=200)
# Add color bar
fig.colorbar(scatter, ax=ax, shrink=0.6, label='Temperature (°F)')
# Add labels
ax.set_xlabel('Temperature (°F)')
ax.set_ylabel('Humidity (%)')
ax.set_zlabel('Elevation (m)')
ax.set_title('3D Scatter Plot of U.S. Cities (Python Matplotlib Example)')
# Add city labels
for i, txt in enumerate(city):
ax.text(temperature[i], humidity[i], elevation[i], txt)
plt.show()You can refer to the screenshot below to see the output.

This Python example shows how to visualize real data in a 3D scatter plot using color to represent temperature. Each city’s name is labeled for clarity.
It’s a great way to analyze multivariate data, for example, comparing how temperature and humidity vary with elevation across U.S. cities.
Method 5: Interactive 3D Scatter Plot in Python
If you’re using Jupyter Notebook, you can make your 3D scatter plot interactive so you can rotate and zoom in real-time.
Here’s how:
%matplotlib notebook
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate data
np.random.seed(100)
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
color = np.sqrt(x**2 + y**2 + z**2)
# Create interactive 3D scatter
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(x, y, z, c=color, cmap='viridis', s=80)
fig.colorbar(scatter, ax=ax, label='Distance Value')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Interactive 3D Scatter Plot in Python')
plt.show()By enabling %matplotlib notebook, you can interact with the plot, rotate, zoom, and explore data points dynamically.
This feature is incredibly useful when you’re presenting data or exploring patterns visually.
Tips for Better 3D Scatter Plots in Python
- Use color maps wisely — choose ones that make differences clear.
- Avoid using too many points; it can make your plot cluttered.
- Label your axes and add a color bar for clarity.
- Use
alphatransparency to make overlapping points visible. - Always test your visualization in dark and light themes for accessibility.
So, that’s how you can create and customize a 3D scatter plot with color in Python using Matplotlib.
From simple static plots to interactive and real-world examples, you now have all the tools to visualize complex data beautifully.
I personally use these techniques in data analysis and presentations because they help make insights stand out instantly.
You may also like to read:
- Create a Stacked Bar Chart with Labels in Python Matplotlib
- Create a Stacked Bar Chart Using a For Loop with Matplotlib
- Create Stacked Bar Chart with Negative Values in Matplotlib
- Create a Horizontal Stacked Bar Chart in Matplotlib

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.