Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to set axis limits in matplotlib

Understanding Axes in Matplotlib

Before diving into setting axis limits, let's first understand what axes are in the context of Matplotlib. Think of axes like the edges of a picture frame. Within this frame, your data is displayed. When you create a plot, Matplotlib automatically scales these edges to fit the data. However, there are times when you want more control over how your data is presented, and that's where setting axis limits comes into play.

Setting Axis Limits Using set_xlim() and set_ylim()

One of the simplest ways to set axis limits in Matplotlib is by using the set_xlim() and set_ylim() methods of the axes object. These methods allow you to define the range of values displayed on the x-axis and y-axis, respectively.

Here's a basic example:

import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.plot(x, y)

# Set the limits for the x-axis
ax.set_xlim(0, 5)

# Set the limits for the y-axis
ax.set_ylim(0, 20)

# Display the plot
plt.show()

In this example, set_xlim(0, 5) ensures that the x-axis starts at 0 and ends at 5, while set_ylim(0, 20) sets the y-axis range from 0 to 20.

Using xlim() and ylim() Functions

Alternatively, Matplotlib provides the xlim() and ylim() functions that can be used to set the axis limits as well. These functions can be used in two ways: either by passing the limits as arguments or by using them to get the current axis limits.

import matplotlib.pyplot as plt

# Sample data
x = [10, 20, 30, 40, 50]
y = [100, 200, 300, 400, 500]

# Plot the data
plt.plot(x, y)

# Set axis limits
plt.xlim(0, 60)
plt.ylim(50, 550)

# Display the plot
plt.show()

This code snippet will produce a plot with the x-axis ranging from 0 to 60 and the y-axis from 50 to 550.

Intuition Behind Axis Limits

Imagine you're using a telescope to look at the stars. Without adjusting the telescope, you might see a broad view of the night sky. But if you're interested in a specific constellation, you'd adjust the telescope to focus on that area. Similarly, setting axis limits allows you to 'zoom in' or 'focus' on particular regions of your data.

Dynamic Axis Limits

Sometimes, you might want to set axis limits dynamically based on the data. Here's how you could do it:

import matplotlib.pyplot as plt

# Sample data
x = [5, 10, 15, 20, 25]
y = [2, 3, 5, 7, 11]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.plot(x, y)

# Dynamically set the x-axis limits to the range of x plus some padding
padding = 5
ax.set_xlim(min(x) - padding, max(x) + padding)

# Dynamically set the y-axis limits to the range of y plus some padding
ax.set_ylim(min(y) - padding, max(y) + padding)

# Display the plot
plt.show()

In this example, the limits are set based on the minimum and maximum values in the data arrays, with some extra space added for padding.

Aspect Ratio and Axis Scaling

Sometimes, you want the axes to be scaled equally, so that one unit on the x-axis is equal to one unit on the y-axis. This can be important for certain types of data, such as geographical data, where you want to maintain the proportionality of the distances represented.

import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.plot(x, y)

# Set the aspect of the plot to be equal
ax.set_aspect('equal')

# Set the limits for both axes
ax.set_xlim(0, 5)
ax.set_ylim(0, 20)

# Display the plot
plt.show()

By setting the aspect ratio to 'equal', we ensure that the units are equally scaled on both axes.

Conclusion

In this blog post, we've walked through the basics of setting axis limits in Matplotlib. By controlling axis limits, you can focus on specific areas of your data, just like how a photographer chooses what to include in a picture frame. We've seen how to use the set_xlim() and set_ylim() methods, as well as the xlim() and ylim() functions, to manually set the axis range. We also touched on dynamic axis limits and the importance of aspect ratio for certain types of data.

Matplotlib provides the flexibility to present your data in the way that best tells its story. Whether you're zooming in on a key trend or ensuring the scale accurately reflects the data's proportions, mastering axis limits is an essential skill in your data visualization toolkit. Just like a skilled artist, you now have the ability to frame your data masterpiece precisely to your liking. Happy plotting!