#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Subplots and Layouts

Learn to create multiple charts in one figure

Subplots and Layouts

Why Multiple Charts?

Sometimes you need to show related charts together:

  • Compare different metrics
  • Show same data different ways
  • Create dashboards

Side by Side (1 Row, 2 Columns)

code.py
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# Left chart
ax1.plot([1, 2, 3], [1, 4, 9])
ax1.set_title('Line Plot')

# Right chart
ax2.bar(['A', 'B', 'C'], [10, 20, 15])
ax2.set_title('Bar Chart')

plt.tight_layout()
plt.show()

Stacked (2 Rows, 1 Column)

code.py
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))

ax1.plot([1, 2, 3], [1, 4, 9])
ax1.set_title('Top Chart')

ax2.bar(['A', 'B', 'C'], [10, 20, 15])
ax2.set_title('Bottom Chart')

plt.tight_layout()
plt.show()

Grid (2x2)

code.py
fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# Top left
axes[0, 0].plot([1, 2, 3], [1, 4, 9])
axes[0, 0].set_title('Line')

# Top right
axes[0, 1].bar(['A', 'B'], [5, 10])
axes[0, 1].set_title('Bar')

# Bottom left
axes[1, 0].scatter([1, 2, 3], [3, 2, 1])
axes[1, 0].set_title('Scatter')

# Bottom right
axes[1, 1].hist([1, 1, 2, 3, 3, 3, 4])
axes[1, 1].set_title('Histogram')

plt.tight_layout()
plt.show()

Share X or Y Axis

code.py
# Same Y axis for both charts
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

ax1.bar(['A', 'B'], [10, 20])
ax2.bar(['C', 'D'], [15, 25])

plt.show()

Different Sized Subplots

code.py
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(12, 6))

# Big chart on left (takes 2/3 width)
ax1 = fig.add_subplot(1, 3, (1, 2))
ax1.plot([1, 2, 3], [1, 4, 9])
ax1.set_title('Main Chart')

# Small chart on right (takes 1/3 width)
ax2 = fig.add_subplot(1, 3, 3)
ax2.bar(['A', 'B'], [5, 10])
ax2.set_title('Side Chart')

plt.tight_layout()
plt.show()

Add Overall Title

code.py
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

ax1.plot([1, 2, 3], [1, 4, 9])
ax2.bar(['A', 'B', 'C'], [10, 20, 15])

fig.suptitle('My Dashboard', fontsize=16, fontweight='bold')

plt.tight_layout()
plt.show()

Loop Through Subplots

code.py
import matplotlib.pyplot as plt

fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# Flatten 2D array to 1D for easy looping
axes_flat = axes.flatten()

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
titles = ['Chart 1', 'Chart 2', 'Chart 3', 'Chart 4']

for ax, d, title in zip(axes_flat, data, titles):
    ax.plot(d)
    ax.set_title(title)

plt.tight_layout()
plt.show()

Complete Dashboard Example

code.py
import matplotlib.pyplot as plt
import numpy as np

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [100, 120, 115, 140, 160, 180]
costs = [80, 85, 90, 95, 100, 110]

fig, axes = plt.subplots(2, 2, figsize=(12, 10))

# Sales trend
axes[0, 0].plot(months, sales, marker='o', color='green')
axes[0, 0].set_title('Sales Trend')
axes[0, 0].set_ylabel('Sales ($K)')

# Cost trend
axes[0, 1].plot(months, costs, marker='s', color='red')
axes[0, 1].set_title('Cost Trend')
axes[0, 1].set_ylabel('Costs ($K)')

# Profit
profit = [s - c for s, c in zip(sales, costs)]
axes[1, 0].bar(months, profit, color='blue')
axes[1, 0].set_title('Monthly Profit')
axes[1, 0].set_ylabel('Profit ($K)')

# Comparison
x = np.arange(len(months))
width = 0.35
axes[1, 1].bar(x - width/2, sales, width, label='Sales')
axes[1, 1].bar(x + width/2, costs, width, label='Costs')
axes[1, 1].set_xticks(x)
axes[1, 1].set_xticklabels(months)
axes[1, 1].legend()
axes[1, 1].set_title('Sales vs Costs')

fig.suptitle('Business Dashboard', fontsize=16, fontweight='bold')
plt.tight_layout()
plt.show()

Key Points

  • plt.subplots(rows, cols) creates grid
  • Access with axes[row, col] or unpack
  • sharey=True shares Y axis
  • tight_layout() prevents overlap
  • fig.suptitle() adds overall title
  • flatten() helps looping through axes

What's Next?

Learn Seaborn - an easier library for beautiful statistical charts.

SkillsetMaster - AI, Web Development & Data Analytics Courses