Instagram
youtube
Facebook
Twitter

Add the Overall Title Above All Subplots

Description:
This program creates a figure with three vertically arranged subplots for Sales, Revenue, and Units Sold, and adds a main title above all subplots using plt.suptitle().

Code Explanation:

  • We have 3 types of data: Sales, Revenue, and Units Sold.

  • We use plt.subplots() to create 3 stacked plots.

  • Each subplot shows a different line graph for each metric.

  • plt.suptitle() adds one big title on top of all subplots — like a headline.

  • plt.tight_layout(rect=[0, 0, 1, 0.95]) keeps spacing clean and avoids overlap with the main title.

  • It's useful when you want to summarize multiple plots under one main topic.


Program:

import matplotlib.pyplot as plt
import pandas as pd

# Sample data
data = {
    'Date': pd.date_range(start='2024-01-01', periods=7, freq='D'),
    'Sales': [100, 120, 90, 140, 160, 130, 150],
    'Revenue': [1000, 1500, 1200, 1800, 2000, 1700, 1900],
    'Units': [10, 12, 9, 14, 16, 13, 15]
}
df = pd.DataFrame(data)

# Create subplots
fig, axs = plt.subplots(3, 1, figsize=(10, 8), sharex=True)

# Plot each metric
axs[0].plot(df['Date'], df['Sales'], color='blue', marker='o')
axs[0].set_title('Sales Over Time')
axs[1].plot(df['Date'], df['Revenue'], color='green', marker='s')
axs[1].set_title('Revenue Over Time')
axs[2].plot(df['Date'], df['Units'], color='red', marker='^')
axs[2].set_title('Units Sold Over Time')

# Rotate x-axis labels
plt.xticks(rotation=45)

# Add overall title
plt.suptitle('Business Metrics Over Time', fontsize=16, fontweight='bold')

# Adjust layout
plt.tight_layout(rect=[0, 0, 1, 0.95])  # Leave space for the suptitle

# Show the plot
plt.show()


Output: