While working on a Python data visualization project, I needed to create a 3D scatter plot that looked more realistic and visually appealing. The challenge was that when I customized the colors of each data point, the natural depth effect, the shading that makes closer points appear brighter, disappeared.
If you’ve ever worked with 3D plots in Matplotlib, you’ve probably noticed how the depthshade parameter can make or break your visualization. In this tutorial, I’ll walk you through everything I’ve learned about using depthshade in Matplotlib 3D scatter plots.
By the end of this article, you’ll not only understand what depthshade does but also how to control it effectively for better results in your Python data visualizations. I’ll also share a few practical examples and Python code snippets that you can run right away.
What is Depthshade in Matplotlib 3D Scatter?
In Matplotlib, when you create a 3D scatter plot using the ax.scatter() or ax.scatter3D() function, there’s a parameter called depthshade. This parameter controls whether the colors of the plotted points are shaded according to their depth relative to the viewer.
When depthshade=True, points that are farther away appear slightly darker, giving a sense of depth and realism. When depthshade=False, all points appear flat, regardless of their distance from the camera.
This shading effect is subtle but powerful; it helps our brain interpret the 3D structure of the data more clearly.
Create a Simple 3D Scatter Plot in Python
Before we explore how depthshade works, let’s first create a basic 3D scatter plot using Python’s Matplotlib library. I’ll use random data here to simulate a real-world dataset, similar to what you might see in a U.S. sales or logistics analysis.
Here’s the full Python code:
# Importing necessary libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating sample data
np.random.seed(42)
x = np.random.rand(50) * 100 # X-axis: Sales in thousands
y = np.random.rand(50) * 50 # Y-axis: Number of stores
z = np.random.rand(50) * 200 # Z-axis: Revenue in thousands
# Creating a 3D figure
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# Plotting a 3D scatter plot
scatter = ax.scatter(x, y, z, c='skyblue', s=60, depthshade=True)
# Adding labels
ax.set_title("3D Scatter Plot with Depthshade in Python", fontsize=14)
ax.set_xlabel("Sales (in $1000s)")
ax.set_ylabel("Number of Stores")
ax.set_zlabel("Revenue (in $1000s)")
# Display the plot
plt.show()You can refer to the screenshot below to see the output.

This code creates a simple 3D scatter plot using random data points. Because we’ve set depthshade=True, you’ll notice that the points closer to the camera appear slightly brighter.
This small detail makes your visualization look more realistic and helps viewers interpret the data’s depth more intuitively.
Turn Off Depthshade in Python Matplotlib
Now, let’s see what happens when we turn off the depthshade property. Sometimes, you might want to disable it, especially when you’re using custom colors or transparency.
Here’s the modified Python code:
# 3D Scatter Plot without Depthshade
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# Plotting points with depthshade turned off
scatter = ax.scatter(x, y, z, c='orange', s=60, depthshade=False)
# Adding labels
ax.set_title("3D Scatter Plot without Depthshade in Python", fontsize=14)
ax.set_xlabel("Sales (in $1000s)")
ax.set_ylabel("Number of Stores")
ax.set_zlabel("Revenue (in $1000s)")
plt.show()You can refer to the screenshot below to see the output.

When you run this code, you’ll notice that all the points have the same brightness level.
This makes the plot look flatter and less three-dimensional, especially when the data overlaps.
Use Custom Colors with Depthshade in Matplotlib
One common issue Python developers face is that when they assign custom colors to each point, the depth shading effect sometimes disappears. That’s because Matplotlib applies depthshade only when it controls the colors internally.
To combine both custom colors and depth shading, you can still use depthshade=True, but you’ll need to ensure your colors are compatible with Matplotlib’s shading logic.
Here’s an example where I use a color map to assign colors based on the Z-axis values:
# Using custom colors with depthshade
from matplotlib import cm
# Create a color map based on Z values
colors = cm.viridis((z - z.min()) / (z.max() - z.min()))
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# Plot with custom colors and depthshade enabled
scatter = ax.scatter(x, y, z, c=colors, s=70, depthshade=True)
ax.set_title("3D Scatter Plot with Custom Colors and Depthshade", fontsize=14)
ax.set_xlabel("Sales (in $1000s)")
ax.set_ylabel("Number of Stores")
ax.set_zlabel("Revenue (in $1000s)")
plt.show()You can refer to the screenshot below to see the output.

This approach gives you both the visual depth and the ability to color points based on data values. It’s perfect for analyzing trends like sales performance across different U.S. regions or product categories.
Adjust Transparency and Depthshade
Another interesting experiment is to combine transparency (alpha values) with depth shading. However, when you use transparency, the depthshade effect might not behave as expected.
Here’s an example that demonstrates this:
# Combining transparency with depthshade
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(x, y, z, c='green', s=60, alpha=0.6, depthshade=True)
ax.set_title("3D Scatter Plot with Transparency and Depthshade in Python", fontsize=14)
ax.set_xlabel("Sales (in $1000s)")
ax.set_ylabel("Number of Stores")
ax.set_zlabel("Revenue (in $1000s)")
plt.show()When you add transparency, the points may look slightly washed out. If you notice that the depth effect is too subtle, try adjusting the alpha value or turning off depthshade to compare the results. Practical Example: Visualize U.S. City Data in 3D
Let’s take a more practical example that reflects real-world data. Suppose you’re analyzing sales performance across major U.S. cities, New York, Los Angeles, Chicago, and so on.
You can use a 3D scatter plot to visualize Sales, Customer Count, and Profit Margin simultaneously.
Here’s how you can do it in Python:
# Practical example: U.S. city data visualization
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Dallas', 'San Diego', 'San Jose']
sales = [120, 95, 80, 75, 60, 70, 65, 55]
customers = [300, 250, 220, 200, 180, 190, 170, 160]
profit_margin = [20, 18, 15, 14, 12, 13, 11, 10]
fig = plt.figure(figsize=(9, 7))
ax = fig.add_subplot(111, projection='3d')
# Using color mapping for profit margin
colors = cm.plasma((np.array(profit_margin) - min(profit_margin)) / (max(profit_margin) - min(profit_margin)))
scatter = ax.scatter(sales, customers, profit_margin, c=colors, s=200, depthshade=True)
# Adding labels
ax.set_title("3D Sales Visualization with Depthshade (U.S. Cities)", fontsize=15)
ax.set_xlabel("Sales (in $1000s)")
ax.set_ylabel("Number of Customers")
ax.set_zlabel("Profit Margin (%)")
# Annotating city names
for i, city in enumerate(cities):
ax.text(sales[i], customers[i], profit_margin[i], city, fontsize=9)
plt.show()This visualization gives a clear, three-dimensional perspective of how different cities perform. The depthshade parameter enhances the realism, helping you quickly identify which cities stand out.
When Should You Use or Avoid Depthshade in Python?
From my experience, depthshade=True works best when you have dense 3D data and want to emphasize spatial relationships. However, if you’re using transparency or need precise color control, you might prefer depthshade=False.
Here’s a quick summary:
| Scenario | Recommended Setting |
|---|---|
| Default 3D scatter plots | depthshade=True |
| Custom colors + shading | depthshade=True |
| Transparent points | depthshade=False |
| Performance optimization | depthshade=False |
Common Issues with Depthshade and Their Fixes
- Colors look flat even with depthshade=True:
Make sure you’re not using fully opaque custom colors that override shading. - Transparency not rendering correctly:
Try disablingdepthshadeor lowering alpha slightly. - Depth ordering looks incorrect:
Use smaller marker sizes or adjust the viewing angle with ax.view_init().
Depth shading might seem like a small visual detail, but it makes a big difference in how your 3D scatter plots look and feel. In my 10+ years of working with Python and Matplotlib, I’ve found that enabling depthshade can transform a flat chart into a visually engaging story.
Whether you’re visualizing financial data, geographic insights, or scientific results, remember that depth perception helps your audience understand your data faster.
Experiment with both options, depthshade=True and depthshade=False, and see what works best for your visualization.
You may also like to read:
- Create a Matplotlib 3D Scatter Animation in Python
- Set Xlim and Zlim in Matplotlib 3D Scatter Plot
- Create 3D Scatter Plot from a NumPy Array in Matplotlib
- Change View Angle in Matplotlib 3D Scatter Plot in Python

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.