Matplotlib is a powerful data visualization library for Python that provides a comprehensive set of tools for creating high-quality 2D and 3D plots. One of the key features of Matplotlib is its ability to create subplots, which allow you to display multiple plots in a single figure. In this tutorial, we will explore how to customize the axes limits of subplots in Matplotlib.
Introduction to Subplots
Subplots are a great way to compare and contrast different data sets or to show the relationship between different variables. To create a subplot in Matplotlib, you can use the subplots
function, which returns a figure object and an array of axis objects. For example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 1, figsize=(9, 6))
This code creates a figure with two subplots arranged vertically.
Setting Axes Limits
To set the axes limits of a subplot, you can use the set_xlim
and set_ylim
methods of the axis object. For example:
ax[0].set_xlim(0, 10)
ax[0].set_ylim(0, 100)
This code sets the x-axis limit to (0, 10) and the y-axis limit to (0, 100) for the first subplot.
Setting Axes Limits for Multiple Subplots
If you have multiple subplots, you can use a loop to set the axes limits for each subplot. For example:
fig, ax = plt.subplots(4, 2)
for i in range(4):
for j in range(2):
ax[i, j].set_ylim(0, 100)
This code sets the y-axis limit to (0, 100) for all subplots.
Using the ylim
Argument
Alternatively, you can use the ylim
argument when creating a subplot to set the y-axis limit. For example:
plt.subplot(2, 1, 2, ylim=(0, 100))
This code creates a subplot with a y-axis limit of (0, 100).
Using the Object-Oriented Interface
Matplotlib also provides an object-oriented interface for creating plots. You can use this interface to set the axes limits and other properties of a plot. For example:
fig, (ax1, ax2) = plt.subplots(2, figsize=(9, 6))
ax1.plot([1, 2, 3])
ax1.set(title='Signal', ylim=(0, 10))
ax2.plot([4, 5, 6])
ax2.set(title='FFT', ylim=(0, 100))
This code creates a figure with two subplots and sets the title and y-axis limit for each subplot.
Example Use Case
Here is an example use case that demonstrates how to create a plot with multiple subplots and customize the axes limits:
import matplotlib.pyplot as plt
import numpy as np
# Create some sample data
x = np.arange(1000)
y1 = np.random.rand(1000)
y2 = np.random.rand(1000)
# Create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(2, figsize=(9, 6))
# Plot the data in each subplot
ax1.plot(x, y1)
ax2.plot(x, y2)
# Set the title and axes limits for each subplot
ax1.set(title='Signal', ylim=(0, 1))
ax2.set(title='FFT', ylim=(0, 10))
# Show the plot
plt.show()
This code creates a figure with two subplots and sets the title and y-axis limit for each subplot.
Conclusion
In this tutorial, we have explored how to customize the axes limits of subplots in Matplotlib. We have seen how to use the set_xlim
and set_ylim
methods, as well as the ylim
argument, to set the axes limits for individual subplots or multiple subplots. We have also demonstrated how to use the object-oriented interface to create plots and set their properties. By following these examples and techniques, you can create high-quality plots with customized axes limits using Matplotlib.