Zooming in 3D Scatter Plots in Python Matplotlib

I was working on a data visualisation project where I had to analyse customer purchase data across different U.S. states. I wanted to represent the data in a 3D scatter plot using Python Matplotlib so that I could easily see the relationship between income, spending, and satisfaction levels.

While creating the plot was easy, I quickly realised that zooming in and out interactively in a 3D scatter plot wasn’t as easy as I expected. There’s no direct “zoom” button in Matplotlib like you might find in Excel charts or BI tools.

So, I started exploring different ways to control zoom levels in a Matplotlib 3D scatter plot, both interactively and programmatically. In this article, I’ll share everything I learned, including multiple methods you can use to zoom effectively in 3D scatter plots.

What is a 3D Scatter Plot in Python Matplotlib?

A 3D scatter plot is a type of visualisation that displays points in three dimensions, X, Y, and Z. It’s commonly used to visualise relationships between three numerical variables.

In Python, we can easily create 3D scatter plots using Matplotlib’s Axes3D module. Once created, you can zoom, rotate, and pan the plot to explore the data from different perspectives.

Let’s start by creating a simple 3D scatter plot before we move on to zooming.

Create a Basic 3D Scatter Plot in Python

Before we talk about zooming, let’s create a simple 3D scatter plot using Matplotlib.

Here’s the full code example:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generate random data for demonstration
np.random.seed(42)
x = np.random.randint(1000, 5000, 50)  # Income
y = np.random.randint(200, 1000, 50)   # Monthly Spend
z = np.random.randint(1, 10, 50)       # Satisfaction Score

# Create a 3D scatter plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

# Plot the points
scatter = ax.scatter(x, y, z, c=z, cmap='viridis', s=50)

# Add labels and title
ax.set_xlabel('Annual Income (USD)')
ax.set_ylabel('Monthly Spend (USD)')
ax.set_zlabel('Satisfaction Score')
ax.set_title('Customer Spending vs Income vs Satisfaction')

plt.show()

This code creates a colourful 3D scatter plot showing how spending and satisfaction vary with income. Now that we have the plot ready, let’s look at how to zoom in and out of it.

Method 1 – Zoom Manually Using Mouse Scroll in Python Matplotlib

When you display a 3D scatter plot in a Matplotlib interactive window, you can zoom manually using your mouse scroll wheel.

Simply hover over the plot and scroll up to zoom in or down to zoom out. This works best when using interactive backends like TkAgg, Qt5Agg, or MacOSX.

If you’re running this in Jupyter Notebook, you can enable interactive zooming by using the %matplotlib notebook magic command:

%matplotlib notebook

Then, run your 3D scatter code again. You’ll be able to zoom, rotate, and pan interactively. This is the easiest way to explore your 3D data visually without writing extra code.

Method 2 – Adjust Zoom Programmatically Using ax.dist

If you want to control the zoom level programmatically, you can use the ax.dist attribute in Matplotlib. This attribute sets the distance between the viewer and the plot. A smaller value zooms in, and a larger value zooms out.

Here’s how you can use it:

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.randint(1000, 5000, 50)
y = np.random.randint(200, 1000, 50)
z = np.random.randint(1, 10, 50)

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

# Plot scatter
scatter = ax.scatter(x, y, z, c=z, cmap='plasma', s=60)

# Set labels
ax.set_xlabel('Income')
ax.set_ylabel('Spend')
ax.set_zlabel('Satisfaction')

# Set zoom level
ax.dist = 7  # Try changing this to 5, 10, or 12 to see the effect

plt.show()

You can refer to the screenshot below to see the output.

Zooming in 3D Scatter Plots in Matplotlib

In this example, reducing ax.dist from the default value (usually around 10) will zoom in closer to the data points. Increasing it will zoom out.

This method is great when you want to create static plots with a specific zoom level, for example, when preparing visualisations for reports or presentations.

Method 3 – Use ax.set_box_aspect() for Zoomed Scaling

Another useful trick for zooming is adjusting the aspect ratio of the 3D plot. The set_box_aspect() function allows you to stretch or compress the axes, which can give a zoom-like effect.

Here’s how you can do it:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Data
np.random.seed(42)
x = np.random.randint(1000, 5000, 50)
y = np.random.randint(200, 1000, 50)
z = np.random.randint(1, 10, 50)

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x, y, z, c=y, cmap='cool', s=70)

ax.set_xlabel('Income')
ax.set_ylabel('Spend')
ax.set_zlabel('Satisfaction')

# Adjust aspect ratio to zoom
ax.set_box_aspect([1, 1, 0.5])  # Compress Z-axis for zoom-like effect

plt.show()

You can refer to the screenshot below to see the output.

Zooming in 3D Scatter Plots Matplotlib

By changing the ratio values, you can make the plot appear more zoomed in on certain axes. This method is particularly helpful when one axis has a much larger range than the others.

Method 4 – Zoom Using Camera Elevation and Azimuth

In 3D visualisations, zooming often goes hand in hand with changing the camera angle. You can use ax.view_init() to set the elevation (vertical angle) and azimuth (horizontal rotation).

Here’s an example:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

np.random.seed(42)
x = np.random.randint(1000, 5000, 50)
y = np.random.randint(200, 1000, 50)
z = np.random.randint(1, 10, 50)

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x, y, z, c=z, cmap='inferno', s=60)

ax.set_xlabel('Income')
ax.set_ylabel('Spend')
ax.set_zlabel('Satisfaction')

# Adjust camera angle
ax.view_init(elev=25, azim=35)

# Adjust zoom
ax.dist = 6

plt.show()

You can refer to the screenshot below to see the output.

Zooming in 3D Scatter Plots in Python Matplotlib

You can experiment with different combinations of elev, azim, and dist to get the perfect zoom and viewing angle.

This method gives you full control over how your 3D scatter plot looks, ideal for creating professional-quality visualisations.

Method 5 – Interactive Zoom with Matplotlib Widgets (Advanced)

If you want to give users control to zoom in and out dynamically (for example, in a dashboard or interactive app), you can use Matplotlib widgets like sliders.

Here’s an example that lets you zoom interactively using a slider:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.widgets import Slider
import numpy as np

np.random.seed(42)
x = np.random.randint(1000, 5000, 50)
y = np.random.randint(200, 1000, 50)
z = np.random.randint(1, 10, 50)

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

scatter = ax.scatter(x, y, z, c=z, cmap='viridis', s=60)

ax.set_xlabel('Income')
ax.set_ylabel('Spend')
ax.set_zlabel('Satisfaction')

ax.dist = 10

# Add slider for zoom control
ax_slider = plt.axes([0.25, 0.02, 0.5, 0.03])
zoom_slider = Slider(ax_slider, 'Zoom', 5, 15, valinit=10)

def update(val):
    ax.dist = zoom_slider.val
    plt.draw()

zoom_slider.on_changed(update)

plt.show()

This approach gives you a real-time zoom control right under your 3D scatter plot. It’s a great way to make your visualisations more interactive, especially for Python dashboards or analytical tools.

Common Mistakes When Zooming in 3D Scatter Plots

  • Using plt.axis(‘equal’) — This doesn’t work with 3D plots. Use set_box_aspect() instead.
  • Forgetting to set interactive mode — Without %matplotlib notebook, mouse zoom won’t work in Jupyter.
  • Setting ax.dist too low — A very small value can distort the perspective view.

Zooming in a Matplotlib 3D scatter plot in Python can make a huge difference when exploring complex datasets. Whether you’re analysing customer data, geographical information, or scientific measurements, being able to control the zoom level helps you spot trends that might otherwise go unnoticed.

You can start with simple mouse-based zooming for quick exploration or use ax.dist for precise, repeatable zoom levels. If you’re building interactive dashboards, adding sliders can make your plots more dynamic and user-friendly.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.