Set Xlim and Zlim in Matplotlib 3D Scatter Plot

While working on a 3D data visualization project for a U.S.-based analytics firm, I had to fine-tune the view of a Matplotlib 3D scatter plot to focus on specific data regions. The problem was that the default axis limits didn’t clearly highlight the patterns I wanted to show. That’s when I realized how powerful it is to control the xlim and zlim in a 3D scatter plot using Python.

In this tutorial, I’ll walk you through two simple methods to set xlim and zlim in a Matplotlib 3D scatter plot. These methods are practical, beginner-friendly, and based on my firsthand experience as a Python developer with hands-on data visualization work.

By the end of this article, you’ll not only understand how to set axis limits in a 3D scatter plot but also how to make your Python plots more readable and presentation-ready for business reports or data dashboards.

Xlim and Zlim in Python Matplotlib 3D Scatter Plots

Before diving into the methods, let’s quickly understand what xlim and zlim mean in the context of Matplotlib 3D scatter plots.

In Python’s Matplotlib library, xlim defines the range of values displayed along the X-axis, while zlim defines the range for the Z-axis. When working with 3D scatter plots, adjusting these limits allows you to zoom in or out on specific sections of your data, making your visualization more focused and informative.

For example, if you’re analyzing sales data across states in the USA, you might want to focus only on a specific region or a range of sales values. Setting custom xlim and zlim helps you do exactly that.

Create a Basic 3D Scatter Plot in Python

Before we apply the xlim and zlim, let’s first create a simple 3D scatter plot using Python and Matplotlib. This will serve as our base example.

Here’s the full code:

# Import necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generate random data for the scatter plot
np.random.seed(42)
x = np.random.randint(0, 100, 50)
y = np.random.randint(0, 100, 50)
z = np.random.randint(0, 100, 50)

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

# Create the scatter plot
ax.scatter(x, y, z, c='blue', marker='o', s=50)

# Add labels
ax.set_xlabel('X Axis (Sales Volume)')
ax.set_ylabel('Y Axis (Customer Count)')
ax.set_zlabel('Z Axis (Profit Margin)')

# Add a title
ax.set_title('3D Scatter Plot Example in Python')

# Show the plot
plt.show()

This code creates a simple 3D scatter plot with random data points. The next step is to learn how to control the xlim and zlim to focus on specific ranges.

Method 1 – Set Xlim in Matplotlib 3D Scatter Plot using set_xlim()

When working with Python’s Matplotlib, the easiest way to set the X-axis limit in a 3D scatter plot is by using the set_xlim() method. This method allows you to define the minimum and maximum range of the X-axis manually.

Here’s how I usually do it in my projects.

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

# Generate data
np.random.seed(10)
x = np.random.randint(0, 100, 100)
y = np.random.randint(0, 100, 100)
z = np.random.randint(0, 100, 100)

# Create 3D scatter plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='green', marker='^', s=40)

# Set X-axis limits
ax.set_xlim(20, 80)

# Add labels and title
ax.set_xlabel('Sales (in Thousands)')
ax.set_ylabel('Customers')
ax.set_zlabel('Profit Margin (%)')
ax.set_title('Setting Xlim in Matplotlib 3D Scatter Plot (Python Example)')

plt.show()

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

Set Xlim in Matplotlib 3D Scatter Plot

This code restricts the X-axis range between 20 and 80. It helps you zoom into a specific portion of your data, which is especially useful when you want to focus on mid-range values or remove outliers.

I often use this method when visualizing sales data across different U.S. states, where certain states have extreme values that can distort the visualization.

Method 2 – Set Xlim in 3D Scatter Plot using set_xlim3d()

Another way to set the X-axis limits in a Python Matplotlib 3D scatter plot is by using the set_xlim3d() method. While it works similarly to set_xlim(), it’s specifically designed for 3D projections.

Here’s a practical example:

# Import libraries
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.randint(0, 150, 80)
y = np.random.randint(0, 150, 80)
z = np.random.randint(0, 150, 80)

# Create the 3D scatter plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='red', marker='o', s=50)

# Set X-axis limit using set_xlim3d()
ax.set_xlim3d(30, 120)

# Add labels and title
ax.set_xlabel('Revenue (in USD Millions)')
ax.set_ylabel('Customer Retention')
ax.set_zlabel('Market Share (%)')
ax.set_title('Using set_xlim3d() in Python Matplotlib 3D Scatter Plot')

plt.show()

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

Set Xlim 3D Scatter Plot in Matplotlib

This method is especially useful when you’re working with advanced 3D visualizations or when you need to dynamically adjust the axis limits in a Python dashboard or interactive plot.

Method 1 – Set Zlim in Matplotlib 3D Scatter Plot using set_zlim()

Now let’s move on to controlling the Z-axis. Setting the Z-axis limits helps you focus on vertical data variations, for instance, profit margins or growth rates in business analytics.

Here’s how you can use the set_zlim() method in Python:

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

# Generate data
np.random.seed(50)
x = np.random.randint(0, 100, 60)
y = np.random.randint(0, 100, 60)
z = np.random.randint(0, 100, 60)

# Create the 3D scatter plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='orange', marker='s', s=60)

# Set Z-axis limits
ax.set_zlim(10, 70)

# Add labels and title
ax.set_xlabel('Product ID')
ax.set_ylabel('Sales Count')
ax.set_zlabel('Profit Margin (%)')
ax.set_title('Setting Zlim in Matplotlib 3D Scatter Plot using Python')

plt.show()

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

Set Zlim in Matplotlib 3D Scatter Plot

This method is straightforward and perfect for cases where you want to limit the vertical range of your 3D scatter plot. It’s particularly helpful when visualizing business KPIs that vary significantly across different categories.

Method 2 – Set Zlim in 3D Scatter Plot using set_zlim3d()

Just like set_xlim3d(), Matplotlib also offers a set_zlim3d() method for controlling the Z-axis in 3D scatter plots. It’s a more explicit method and often used in advanced Python visualization scripts.

Here’s an example:

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

# Generate random data
np.random.seed(70)
x = np.random.randint(0, 200, 100)
y = np.random.randint(0, 200, 100)
z = np.random.randint(0, 200, 100)

# Create 3D scatter plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='purple', marker='x', s=50)

# Set Z-axis limit using set_zlim3d()
ax.set_zlim3d(50, 150)

# Add labels and title
ax.set_xlabel('Revenue (in USD Thousands)')
ax.set_ylabel('Customer Engagement')
ax.set_zlabel('Profit Growth (%)')
ax.set_title('Using set_zlim3d() in Python Matplotlib 3D Scatter Plot')

plt.show()

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

Set Zlim 3D Scatter Plot in Matplotlib

This approach provides fine-grained control over the Z-axis, enabling you to zoom in on specific data ranges or exclude irrelevant values.

Tips for Setting Xlim and Zlim in Python Matplotlib 3D Scatter Plots

  • Always analyze your data distribution before setting axis limits.
  • Avoid setting limits too narrowly, as this can hide important data points.
  • Combine both xlim and zlim adjustments to create a balanced view.
  • Use consistent axis scaling when comparing multiple 3D scatter plots.

When I first started using Matplotlib for 3D visualizations, I often overlooked how axis limits could enhance clarity. Over the years, setting the right xlim and zlim has become a key part of my Python data visualization workflow. Whether you’re presenting business insights or building machine learning dashboards, mastering axis control can significantly improve your visual storytelling.

By now, you’ve learned two effective methods for setting xlim and zlim in Python Matplotlib 3D scatter plots. Both are simple yet powerful ways to make your plots more focused and professional.

Setting axis limits in 3D scatter plots is one of those subtle adjustments that make a significant difference in how your data is perceived. Once you start applying these techniques, you’ll notice how much cleaner and more insightful your Python visualizations become.

You may also 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.