When I first started working with Python Matplotlib, I often struggled with making my subplot layouts look neat.
Sometimes, the plots looked too cramped, and other times, the lack of grid lines made it hard to read the data. I’ve learned some simple but powerful tricks to control subplot grid lines and grid spacing in Python.
In this tutorial, I’ll share my firsthand experience with you. I’ll show you exactly how to add grid lines to subplots and how to adjust the spacing between them.
Adding Grid Lines to Subplots in Python
Grid lines make it easier to read values from a chart. In Python Matplotlib, enabling grid lines on subplots only takes one line of code.
I’ll show you a few methods I personally use, depending on the project.
Method 1 – Using ax.grid() for Individual Subplots
When I want to control grid lines for each subplot separately, I use the ax.grid() method. Here’s a simple example with U.S. population data for three states.
import matplotlib.pyplot as plt
# Sample data: population in millions
years = [2010, 2015, 2020, 2025]
california = [37.3, 38.8, 39.5, 40.2]
texas = [25.1, 27.4, 29.1, 30.0]
florida = [18.8, 20.3, 21.5, 22.2]
fig, axs = plt.subplots(1, 3, figsize=(12, 4))
# California
axs[0].plot(years, california, marker='o')
axs[0].set_title("California Population")
axs[0].grid(True)
# Texas
axs[1].plot(years, texas, marker='o', color='orange')
axs[1].set_title("Texas Population")
axs[1].grid(True, linestyle='--')
# Florida
axs[2].plot(years, florida, marker='o', color='green')
axs[2].set_title("Florida Population")
axs[2].grid(True, color='gray', alpha=0.7)
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

In this example, I enabled grid lines for each subplot. I also customized the style, color, and transparency. This method is great when different subplots need different grid styles.
Method 2 – Enable Grid Lines for All Subplots in a Loop
Sometimes, I want all subplots to have the same grid lines. Instead of repeating code, I loop through each axis.
import matplotlib.pyplot as plt
# Sample data for U.S. GDP in trillions
years = [2010, 2015, 2020, 2025]
gdp = [15.0, 18.0, 21.4, 25.0]
gdp_growth = [2.5, 2.9, 2.3, 2.7]
inflation = [1.6, 0.1, 1.2, 2.0]
fig, axs = plt.subplots(1, 3, figsize=(12, 4))
data = [gdp, gdp_growth, inflation]
titles = ["U.S. GDP", "GDP Growth (%)", "Inflation (%)"]
for i, ax in enumerate(axs):
ax.plot(years, data[i], marker='o')
ax.set_title(titles[i])
ax.grid(True, linestyle=':', color='blue')
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

Here, I applied the same grid style to all subplots using a simple loop. This method saves time and keeps the plots consistent.
Method 3 – Add Major and Minor Grid Lines
For financial or scientific data, I sometimes need both major and minor grid lines.
import matplotlib.pyplot as plt
# U.S. unemployment rate
years = [2010, 2012, 2014, 2016, 2018, 2020]
unemployment = [9.6, 8.1, 6.2, 4.9, 3.9, 8.1]
fig, ax = plt.subplots()
ax.plot(years, unemployment, marker='o')
ax.set_title("U.S. Unemployment Rate")
# Major and minor grid lines
ax.minorticks_on()
ax.grid(True, which='major', linestyle='-', linewidth=0.8)
ax.grid(True, which='minor', linestyle=':', linewidth=0.5, color='gray')
plt.show()This method gives me more control and makes the chart easier to interpret.
Adjust Subplot Grid Spacing in Python
When I create multiple subplots, sometimes the charts overlap or look too tight. Adjusting subplot grid spacing helps make the figure look clean and professional.
Method 1 – Use plt.subplots_adjust()
The plt.subplots_adjust() method lets me control spacing between subplots.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(8, 6))
for i, ax in enumerate(axs.flat):
ax.plot([1, 2, 3], [j*(i+1) for j in [1, 2, 3]])
ax.set_title(f"Chart {i+1}")
# Adjust spacing
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1,
wspace=0.4, hspace=0.6)
plt.show()You can refer to the screenshot below to see the output.

Here, I used wspace for horizontal spacing and hspace for vertical spacing. This method is perfect when I want precise control over layout.
Method 2 – Use Python’s plt.tight_layout()
When I want Python to automatically fix spacing, I use plt.tight_layout().
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(8, 6))
for i, ax in enumerate(axs.flat):
ax.plot([1, 2, 3], [j*(i+1) for j in [1, 2, 3]])
ax.set_title(f"Chart {i+1}")
# Automatically adjust spacing
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

This method is quick and works well for most cases.
Method 3 – Combine tight_layout() with Padding
Sometimes, even after using tight_layout(), the titles or labels overlap. In that case, I add padding to make it look better.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(8, 6))
for i, ax in enumerate(axs.flat):
ax.plot([1, 2, 3], [j*(i+1) for j in [1, 2, 3]])
ax.set_title(f"Chart {i+1}")
# Add padding
plt.tight_layout(pad=3.0)
plt.show()This gives me a clean result without manually adjusting every parameter.
Working with subplot grid lines and grid spacing in Python Matplotlib used to feel overwhelming for me. But once I learned these simple methods, my plots became much easier to read and more professional-looking.
Whenever I need precision, I use plt.subplots_adjust(). When I want a quick fix, I rely on plt.tight_layout(). And for grid lines, I always decide whether I need simple grids, consistent grids across subplots, or both major and minor grids.
You may also like to read:
- Matplotlib Subplot Titles – Change Font Size and Bold Text
- Matplotlib Subplot Title Style – Change Position and Padding
- Share Axis and Axis Labels in Matplotlib Subplots
- Add Legends in Matplotlib Subplots Using 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.