Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Setting active subplot using axes object in Matplotlib
To set an active subplot using axes objects in Matplotlib, you can use the plt.axes() function to switch between different subplot axes. This allows you to work with multiple subplots and control which one is currently active for plotting operations.
Syntax
fig, axs = plt.subplots(rows, cols) plt.axes(axs[index]) # Set specific subplot as active
Basic Example
Here's how to create subplots and switch between them using axes objects ?
import matplotlib.pyplot as plt
# Set figure configuration
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = [1, 2, 4, 2, 1]
y = [1, 4, 0, 4, 1]
# Create figure with 1 row and 2 columns of subplots
fig, axs = plt.subplots(1, 2)
# Set first subplot as active and plot
plt.axes(axs[0])
plt.plot(x, y)
plt.title("Plot 1: x vs y")
# Set second subplot as active and plot
plt.axes(axs[1])
plt.plot(y, x)
plt.title("Plot 2: y vs x")
plt.show()
Working with Grid Layout
You can also work with more complex subplot arrangements ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
# Create 2x2 grid of subplots
fig, axs = plt.subplots(2, 2, figsize=(8, 6))
# Plot in top-left subplot
plt.axes(axs[0, 0])
plt.plot(x, y1)
plt.title("sin(x)")
# Plot in top-right subplot
plt.axes(axs[0, 1])
plt.plot(x, y2)
plt.title("cos(x)")
# Plot in bottom-left subplot
plt.axes(axs[1, 0])
plt.plot(x, y3)
plt.title("tan(x)")
plt.ylim(-2, 2)
# Leave bottom-right subplot empty
plt.axes(axs[1, 1])
plt.text(0.5, 0.5, 'Empty subplot', ha='center', va='center', transform=axs[1,1].transAxes)
plt.tight_layout()
plt.show()
Alternative Approach: Direct Axes Method
Instead of using plt.axes(), you can call methods directly on the axes objects ?
import matplotlib.pyplot as plt
# Create data
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 1, 5, 3]
y2 = [1, 3, 5, 2, 4]
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
# Plot directly on each axis
ax1.plot(x, y1, 'b-o')
ax1.set_title("First Subplot")
ax1.set_xlabel("X values")
ax1.set_ylabel("Y values")
ax2.plot(x, y2, 'r-s')
ax2.set_title("Second Subplot")
ax2.set_xlabel("X values")
ax2.set_ylabel("Y values")
plt.tight_layout()
plt.show()
Key Points
plt.axes(axs[index])makes a specific subplot active for subsequent plotting commandsThe axes object can be accessed using indexing:
axs[0]for 1D,axs[row, col]for 2DDirect method calls on axes objects (like
ax.plot()) are often preferred for cleaner codeUse
plt.tight_layout()to automatically adjust spacing between subplots
Conclusion
Using plt.axes() with axes objects provides flexible control over multiple subplots in Matplotlib. While this method works well, calling methods directly on individual axes objects is generally more explicit and recommended for complex layouts.
