Sometimes you want to change how each small plot looks inside a bigger picture. This helps show different details clearly.
0
0
Individual subplot customization in Matplotlib
Introduction
You have multiple charts in one figure and want each to have its own title or labels.
You want to change colors or styles for each small plot separately.
You need to highlight or zoom in on one subplot without affecting others.
You want to add different grid lines or legends to each subplot.
You want to adjust axis limits or ticks individually for better clarity.
Syntax
Matplotlib
fig, axs = plt.subplots(nrows, ncols) axs[row, col].plot(data) axs[row, col].set_title('Title') axs[row, col].set_xlabel('X label') axs[row, col].set_ylabel('Y label')
axs is an array of subplot axes you can customize one by one.
Use axs[row, col] to select the subplot at that position.
Examples
Two plots side by side with different titles.
Matplotlib
fig, axs = plt.subplots(1, 2) axs[0].plot([1, 2, 3]) axs[0].set_title('First plot') axs[1].plot([3, 2, 1]) axs[1].set_title('Second plot')
Four plots in a grid, setting labels only on two of them.
Matplotlib
fig, axs = plt.subplots(2, 2) axs[0, 0].set_xlabel('X1') axs[1, 1].set_ylabel('Y2')
Two vertical plots with different line colors.
Matplotlib
fig, axs = plt.subplots(2, 1) axs[0].plot([1, 4, 9], color='red') axs[1].plot([9, 4, 1], color='blue')
Sample Program
This code creates four different plots in one figure. Each plot has its own style and labels. This shows how to change each subplot separately.
Matplotlib
import matplotlib.pyplot as plt # Create 2x2 grid of subplots fig, axs = plt.subplots(2, 2, figsize=(8, 6)) # Plot data and customize each subplot axs[0, 0].plot([1, 2, 3], [1, 4, 9]) axs[0, 0].set_title('Square Numbers') axs[0, 0].set_xlabel('x') axs[0, 0].set_ylabel('x squared') axs[0, 1].plot([1, 2, 3], [1, 8, 27], 'r--') axs[0, 1].set_title('Cube Numbers') axs[0, 1].set_xlabel('x') axs[0, 1].set_ylabel('x cubed') axs[1, 0].bar(['A', 'B', 'C'], [5, 7, 3], color='green') axs[1, 0].set_title('Bar Chart') axs[1, 1].scatter([1, 2, 3], [3, 2, 1], color='purple') axs[1, 1].set_title('Scatter Plot') plt.tight_layout() plt.show()
OutputSuccess
Important Notes
Use plt.tight_layout() to avoid overlapping labels.
Remember to index subplots correctly: for 2D grids use axs[row, col], for 1D arrays use axs[index].
You can customize anything on each subplot like colors, titles, labels, grids, and more.
Summary
You can change each small plot inside a big figure separately.
Use the axes array to pick and customize each subplot.
This helps make your charts clearer and more informative.