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
How to Set a Single Main Title for All the Subplots in Matplotlib?
When creating multiple subplots in Matplotlib, you often need a single main title that spans across all subplots. The suptitle() function provides an elegant solution for setting a main title above all subplots in a figure.
Syntax
The basic syntax for setting a main title across subplots ?
plt.suptitle('Main Title Text')
# or
fig.suptitle('Main Title Text')
Basic Example with Line Plots
Let's create a 2x2 grid of subplots with different mathematical functions and a single main title ?
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.array([1, 2, 3, 4, 5])
# Create subplots
fig, ax = plt.subplots(2, 2, figsize=(8, 6))
# Plot different functions in each subplot
ax[0, 0].plot(x, x, 'b-', label='Linear')
ax[0, 1].plot(x, x*x, 'r-', label='Quadratic')
ax[1, 0].plot(x, x**3, 'g-', label='Cubic')
ax[1, 1].plot(x, np.log(x), 'm-', label='Logarithmic')
# Set main title for all subplots
plt.suptitle('Mathematical Functions Comparison', fontsize=16, fontweight='bold')
plt.tight_layout()
plt.show()
Using Image Subplots
You can also use suptitle() with image data displayed using imshow() ?
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10, 8))
# Create sample image data
image_data = np.random.rand(10, 10)
# Create 6 subplots in a 2x3 grid
for i in range(1, 7):
ax = fig.add_subplot(2, 3, i)
ax.imshow(image_data * i, cmap='viridis')
ax.set_title(f'Image {i}')
# Set main title for all subplots
fig.suptitle('Random Image Processing Results', fontsize=14)
plt.tight_layout()
plt.show()
Mixed Plot Types with Pandas Data
Here's an example combining different plot types with pandas DataFrames ?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample DataFrame
df = pd.DataFrame(np.random.rand(10, 4), columns=['A', 'B', 'C', 'D'])
# Create subplots
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Different plot types
df['A'].plot(ax=axes[0,0], kind='line', title='Line Plot')
df['B'].plot(ax=axes[0,1], kind='bar', title='Bar Plot', color='orange')
df['C'].plot(ax=axes[1,0], kind='hist', title='Histogram', color='green')
axes[1,1].scatter(df['C'], df['D'], alpha=0.7, color='red')
axes[1,1].set_title('Scatter Plot')
axes[1,1].set_xlabel('C values')
axes[1,1].set_ylabel('D values')
# Set main title
fig.suptitle('Data Analysis Dashboard', fontsize=16, y=0.98)
plt.tight_layout()
plt.show()
Customizing the Main Title
You can customize the appearance of the main title using various parameters ?
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
# Sample data
x = np.linspace(0, 10, 100)
# Plot in each subplot
axes[0].plot(x, np.sin(x))
axes[1].plot(x, np.cos(x))
axes[2].plot(x, np.tan(x))
# Customized main title
fig.suptitle('Trigonometric Functions',
fontsize=18,
fontweight='bold',
color='darkblue',
y=0.95)
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
fontsize |
Size of the title text | 14, 'large', 'x-large' |
fontweight |
Weight of the font | 'bold', 'normal' |
color |
Color of the title | 'red', 'blue', '#FF5733' |
y |
Vertical position (0-1) | 0.98, 0.95 |
Conclusion
The suptitle() function is essential for creating professional-looking subplot arrangements with a unified main title. Use plt.tight_layout() to prevent overlap and adjust the y parameter to fine-tune title positioning.
