How to add title to subplots in Matplotlib?
0 1569
How to Add Title to Subplots in Matplotlib?
When working with multiple subplots in Matplotlib, it's essential to clearly label each plot. Titles not only improve readability but also provide context for what each subplot represents. Matplotlib makes it simple to set titles on individual subplots or even assign a main title across the entire figure.
Using set_title() for Individual Subplots
The most common way to add a title to a subplot is by using the set_title() method on an Axes object. This allows each subplot to have its own custom title.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(2, 2)
axes[0, 0].plot(x, np.sin(x))
axes[0, 0].set_title('Sine Curve')
axes[0, 1].plot(x, np.cos(x))
axes[0, 1].set_title('Cosine Curve')
axes[1, 0].plot(x, np.tan(x))
axes[1, 0].set_title('Tangent Curve')
axes[1, 1].plot(x, np.exp(-x))
axes[1, 1].set_title('Exponential Decay')
plt.tight_layout()
plt.show()
Each subplot has its own label, helping users quickly identify the type of data visualized.
Using fig.suptitle() for a Main Title
If you want to add one overarching title to the entire figure instead of individual titles, suptitle() is the method to use. This is helpful when all subplots are related and share a common theme.
fig, axs = plt.subplots(1, 2)
axs[0].plot(x, np.sin(x))
axs[0].set_title("Sine")
axs[1].plot(x, np.cos(x))
axs[1].set_title("Cosine")
fig.suptitle("Trigonometric Functions")
plt.tight_layout()
plt.show()
Here, suptitle() places a main title above all the subplots, giving the viewer a clear idea of the figure’s overall context.
Setting Titles Dynamically in Loops
When generating subplots dynamically, especially in loops, you can assign titles based on the data being plotted. This makes the code cleaner and scalable.
functions = [np.sin, np.cos, np.tan, np.exp]
titles = ['Sine', 'Cosine', 'Tangent', 'Exponential']
fig, axs = plt.subplots(2, 2)
for ax, func, title in zip(axs.flat, functions, titles):
ax.plot(x, func(x))
ax.set_title(title)
plt.tight_layout()
plt.show()
This approach works well when dealing with large or repetitive datasets and ensures each subplot is properly labeled.
Common Tips for Better Layout
- Use
plt.tight_layout()after setting titles to automatically adjust spacing. - If titles overlap with each other or with axis labels, consider using
padinsuptitle(). - Limit the number of subplots per figure to maintain readability.
Conclusion
Adding titles to subplots in Matplotlib helps organize your visualizations and communicate insights more effectively. Whether you’re labeling each subplot individually or adding a unified title for the entire figure, Matplotlib provides simple and flexible options to make your plots easier to understand. These small enhancements go a long way in producing polished and professional-looking graphs.
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments