Python:Matplotlib .subplots()
The .subplots() function is a convenient utility in matplotlib.pyplot that creates a figure and a grid of subplots in a single call. It provides an efficient way to set up multiple plotting areas within one figure, allowing for organized visualization of different datasets or various views of the same data. This function is beneficial when creating complex layouts with multiple charts while maintaining consistency in figure management.
The primary use cases for .subplots() include creating side-by-side comparisons of different datasets, displaying multiple views or transformations of the same data, building dashboard-style visualizations with multiple charts, and organizing related plots in a structured grid layout. It’s commonly used in data analysis workflows where multiple related visualizations need to be presented together for comparison or comprehensive analysis.
Syntax
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, width_ratios=None, height_ratios=None, subplot_kw=None, gridspec_kw=None, **fig_kw)
Parameters:
nrows: Number of rows in the subplot grid (default:1)ncols: Number of columns in the subplot grid (default:1)sharex: Controls sharing of x-axis properties among subplots. Can beFalse,True,'all','none','row', or'col'(default:False)sharey: Controls sharing of y-axis properties among subplots. Can beFalse,True,'all','none','row', or'col'(default:False)squeeze: IfTrue, removes single-dimensional entries from the shape of the returned axes object (default:True)width_ratios: Defines relative widths of columns as a sequence of numbersheight_ratios: Defines relative heights of rows as a sequence of numberssubplot_kw: Dictionary of keyword arguments passed toadd_subplot()for each subplotgridspec_kw: Dictionary of keyword arguments passed to theGridSpecconstructor**fig_kw: Additional keyword arguments passed to thepyplot.figure()call
Return value:
The .subplots() function returns a tuple containing:
fig: TheFigureobjectaxoraxs: TheAxesobject(s). For a single subplot, returns a singleAxesobject. For multiple subplots, returns a numpy array ofAxesobjects.
Example 1: Basic Subplot Creation using .subplots()
This example demonstrates the fundamental usage of .subplots() to create a simple 1x2 grid of plots:
import matplotlib.pyplot as pltimport numpy as np# Create sample datax = np.linspace(0, 10, 100)y1 = np.sin(x)y2 = np.cos(x)# Create figure with 1 row and 2 columnsfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))# Plot on first subplotax1.plot(x, y1, 'b-', linewidth=2)ax1.set_title('Sine Wave')ax1.set_xlabel('x')ax1.set_ylabel('sin(x)')ax1.grid(True)# Plot on second subplotax2.plot(x, y2, 'r-', linewidth=2)ax2.set_title('Cosine Wave')ax2.set_xlabel('x')ax2.set_ylabel('cos(x)')ax2.grid(True)# Adjust spacing between subplotsplt.tight_layout()plt.show()
The output generated by this code will be:

This example creates two side-by-side plots showing sine and cosine waves. The .subplots() function returns a figure object and a tuple of two axes objects, which are unpacked into ax1 and ax2. Each subplot can be customized independently using its own title, labels, and grid settings.
Example 2: Sales Data Comparison Dashboard
This example shows how to use .subplots() to create a business dashboard comparing quarterly sales data across different metrics:
import matplotlib.pyplot as pltimport numpy as np# Sample quarterly sales dataquarters = ['Q1', 'Q2', 'Q3', 'Q4']revenue = [125000, 150000, 175000, 200000]units_sold = [1250, 1400, 1600, 1800]profit_margin = [0.15, 0.18, 0.22, 0.25]# Create a 2x2 grid of subplotsfig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 8))# Revenue bar chartax1.bar(quarters, revenue, color='skyblue', alpha=0.7)ax1.set_title('Quarterly Revenue')ax1.set_ylabel('Revenue ($)')ax1.tick_params(axis='y', labelcolor='blue')# Units sold line chartax2.plot(quarters, units_sold, marker='o', color='green', linewidth=3)ax2.set_title('Units Sold per Quarter')ax2.set_ylabel('Units')ax2.grid(True, alpha=0.3)# Profit margin trendax3.plot(quarters, profit_margin, marker='s', color='red', linewidth=2)ax3.set_title('Profit Margin Trend')ax3.set_ylabel('Profit Margin')ax3.set_ylim(0, 0.3)# Combined revenue and units (dual y-axis concept)ax4_twin = ax4.twinx()bars = ax4.bar(quarters, revenue, alpha=0.6, color='lightblue', label='Revenue')line = ax4_twin.plot(quarters, units_sold, 'ro-', label='Units')ax4.set_title('Revenue vs Units Sold')ax4.set_ylabel('Revenue ($)', color='blue')ax4_twin.set_ylabel('Units Sold', color='red')# Adjust layout to prevent overlapplt.tight_layout()plt.show()
The output generated by this code will be:

This example demonstrates a real-world business scenario where multiple related metrics need to be visualized together. The 2x2 grid layout allows for easy comparison of different aspects of sales performance, making it ideal for executive dashboards or quarterly reports.
Example 3: Scientific Data Visualization
This example illustrates using .subplots() for scientific data analysis, showing different mathematical functions and their properties:
import matplotlib.pyplot as pltimport numpy as np# Generate data for mathematical functionsx = np.linspace(-5, 5, 1000)linear = xquadratic = x**2cubic = x**3exponential = np.exp(x/2)# Create 2x2 subplot grid with shared axesfig, axes = plt.subplots(2, 2, figsize=(10, 8), sharex=True)# Flatten axes array for easier iterationaxes = axes.flatten()# Function data and propertiesfunctions = [(linear, 'Linear Function', 'y = x', 'blue'),(quadratic, 'Quadratic Function', 'y = x²', 'red'),(cubic, 'Cubic Function', 'y = x³', 'green'),(exponential, 'Exponential Function', 'y = e^(x/2)', 'purple')]# Plot each functionfor i, (y_data, title, equation, color) in enumerate(functions):axes[i].plot(x, y_data, color=color, linewidth=2, label=equation)axes[i].set_title(title)axes[i].grid(True, alpha=0.3)axes[i].legend()# Add y-label only to left columnif i % 2 == 0:axes[i].set_ylabel('y')# Add x-label only to bottom rowif i >= 2:axes[i].set_xlabel('x')# Set overall titlefig.suptitle('Mathematical Functions Comparison', fontsize=16, fontweight='bold')# Adjust layoutplt.tight_layout()plt.show()
The output generated by this code will be:

This scientific example showcases how .subplots() can be used to compare different mathematical functions in a systematic way. The shared x-axis (sharex=True) ensures all plots use the same x-range, making comparison easier. This approach is common in research papers and educational materials where multiple related functions or datasets need to be presented together.
Frequently Asked Questions
1. What’s the difference between .subplots() and .subplot()?
The .subplots() function creates a figure and all subplots at once, returning the figure and axes objects, while .subplot() creates or selects individual subplots one at a time within the current figure. .subplots() is more convenient for creating multiple subplots simultaneously.
2. How do I access individual subplots when using a 2D grid?
For a 2D grid, the axes are returned as a 2D numpy array. Access them using indices like axes[0, 1] for row 0, column 1, or use axes.flatten() to convert to a 1D array for easier iteration.
3. Can I create subplots with different sizes?
Yes, use the width_ratios and height_ratios parameters to specify relative sizes. For example, width_ratios=[2, 1] makes the first column twice as wide as the second.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Matplotlib on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours