As a Python developer working with Matplotlib for over seven years, I’ve encountered many challenges when it comes to arranging plots neatly. One common issue is dealing with overlapping labels, titles, or legends that make visualizations look cluttered or unprofessional. Thankfully, Matplotlib provides two powerful tools to manage subplot spacing: constrained_layout and tight_layout.
In this article, I’ll share my firsthand experience using both methods in Python. I’ll explain how they work, their differences, and provide you with practical examples so you can decide which one suits your plotting needs best.
Layout Management Matters in Python Plotting
When creating complex visualizations with multiple subplots, ensuring that axes labels, titles, and legends don’t overlap is crucial. Poorly spaced plots can confuse viewers and reduce the clarity of your data story.
Matplotlib offers several ways to control subplot spacing, but constrained_layout and tight_layout are the most commonly used. Both aim to optimize the layout automatically, but they work differently under the hood. Understanding these differences will help you create cleaner and more professional Python plots.
Method 1: Use tight_layout() in Python Matplotlib
I first started using tight_layout() years ago when I noticed my subplot labels were getting cut off or overlapping. This method adjusts subplot params so that subplots fit into the figure area neatly.
How tight_layout() Works
tight_layout() calculates the required padding between subplots and automatically adjusts the subplot parameters. It’s simple to use and works well in most basic scenarios.
Example Code Using tight_layout()
import matplotlib.pyplot as plt
import numpy as np
# Sample data representing monthly sales in the USA
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales_A = [150, 200, 250, 300, 280, 350]
sales_B = [180, 210, 260, 310, 290, 370]
fig, axs = plt.subplots(2, 1, figsize=(8, 6))
axs[0].bar(months, sales_A, color='blue')
axs[0].set_title('Product A Sales')
axs[0].set_ylabel('Units Sold')
axs[1].bar(months, sales_B, color='green')
axs[1].set_title('Product B Sales')
axs[1].set_ylabel('Units Sold')
# Apply tight_layout to avoid overlap
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

When to Use tight_layout()
- Works well for simple figures with few subplots.
- Automatically adjusts padding for axis labels and titles.
- Quick and easy to implement.
Limitations of tight_layout()
- Sometimes it doesn’t handle complex layouts or legends well.
- May not work perfectly with
GridSpecor when using multiple figure elements like colorbars. - It can require manual tweaking with parameters like pad, w_pad, and h_pad.
Method 2: Use constrained_layout in Python Matplotlib
After working with more complex figures, I discovered constrained_layout, introduced in Matplotlib 3.1. It offers a more advanced and robust way to manage subplot spacing.
How constrained_layout Works
constrained_layout automatically adjusts subplots and decorations (like labels, titles, and legends) to minimize overlaps. It considers all figure elements, including colorbars, making it more reliable for complex layouts.
Enable constrained_layout
You activate it by setting constrained_layout=True when creating the figure or subplot.
Example Code Using constrained_layout
import matplotlib.pyplot as plt
import numpy as np
# Sample data for quarterly GDP growth rates in the USA
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
gdp_growth_2019 = [2.3, 2.1, 2.0, 1.8]
gdp_growth_2020 = [-3.5, -31.4, 33.4, 4.0]
fig, axs = plt.subplots(2, 1, figsize=(8, 6), constrained_layout=True)
axs[0].plot(quarters, gdp_growth_2019, marker='o', color='orange')
axs[0].set_title('USA GDP Growth 2019')
axs[0].set_ylabel('Growth (%)')
axs[1].plot(quarters, gdp_growth_2020, marker='o', color='red')
axs[1].set_title('USA GDP Growth 2020')
axs[1].set_ylabel('Growth (%)')
plt.show()I executed the above example code and added the screenshot below.

When to Use constrained_layout
- Best for complex figures with multiple subplots and decorations.
- Handles colorbars, legends, and titles more gracefully.
- Less manual adjustment is needed compared to tight_layout.
Things to Watch Out For
- constrained_layout may conflict with some custom subplot configurations.
- It might not work perfectly with some older Matplotlib versions or specific plot types.
- You cannot combine constrained_layout and tight_layout in the same figure.
Additional Tips and Tricks for Layout Management in Python Matplotlib
Use Parameters with tight_layout()
You can fine-tune tight_layout() by passing parameters like:
pad: Padding between the figure edge and the edges of subplots.- w_pad and h_pad: Padding between subplots horizontally and vertically.
plt.tight_layout(pad=2.0, w_pad=0.5, h_pad=1.0)Combine GridSpec with constrained_layout
When using GridSpec for complex subplot arrangements, enable constrained_layout on the figure level:
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True, figsize=(8, 6))
gs = gridspec.GridSpec(2, 2, figure=fig)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, :])
ax1.set_title('Top Left')
ax2.set_title('Top Right')
ax3.set_title('Bottom')
plt.show()This ensures automatic spacing without overlaps.
My Experience Choosing Between tight_layout and constrained_layout
In my Python projects, I often start with constrained_layout because it handles complex layouts more reliably. It saves me time adjusting subplot parameters manually. However, for quick, simple plots or when I need more control over padding, I sometimes prefer tight_layout.
If your visualization includes colorbars or legends outside the main plot area, constrained_layout usually manages these better. But if you face any issues or conflicts with your specific plot types, switching to tight_layout with manual padding adjustments is a good fallback.
Creating professional, clean plots in Python with Matplotlib is essential for clear data communication. Both constrained_layout and tight_layout serve this purpose, but shine in different scenarios. By understanding their strengths and limitations, you can confidently choose the right method for your next project.
If you want to dive deeper into Matplotlib layouts or explore other Python data visualization tips, keep experimenting with these tools. They will make your plots look polished and ready for presentation or publication.
Related Python Matplotlib Articles You May Like:
- Matplotlib tight_layout wspace and hspace in Python
- Matplotlib Tight_Layout for Python Subplots
- How to Use tight_layout and bbox_inches in Matplotlib
- Matplotlib Scatter Plots with Tight_Layout in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.