Matplotlib Subplot Figure Size in Python

As a developer, I was working on a Python project where I had to create multiple charts in one figure. The problem was that the default subplot sizes in Matplotlib were too small, and the labels were overlapping.

As someone who has been coding in Python, I’ve faced this issue countless times. Thankfully, Matplotlib gives us several ways to control subplot figure size. Once you know these tricks, you can make your plots look neat and professional.

In this tutorial, I’ll show you step-by-step how to adjust subplot figure sizes in Python. I’ll cover different methods so you can pick the one that works best for your project.

What is Subplot Figure Size in Matplotlib?

When we use subplots in Matplotlib, we often place multiple plots inside one figure. By default, Matplotlib decides the figure size for us.

However, in real-world Python projects, the default size is rarely perfect. Titles may overlap, axis labels might look cramped, and the plots may not be readable. That’s where customizing the subplot figure size comes in handy.

Method 1 – Use Python’s figsize in plt.subplots()

The most common way to set the subplot figure size in Python is by using the figsize parameter inside plt.subplots().

This method is quick and works for most cases where you want to define the overall figure size.

import matplotlib.pyplot as plt
import numpy as np

# Sample data for demonstration
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Create subplots with custom figure size
fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Plot data on subplots
axes[0].plot(x, y1, color='blue')
axes[0].set_title("Sine Wave")

axes[1].plot(x, y2, color='red')
axes[1].set_title("Cosine Wave")

# Show the plots
plt.show()

I executed the above example code and added the screenshot below.

Matplotlib Subplot Figure Size Python

In this example, I set the figure size to (12, 5). This makes the plots wider, giving enough space for both sine and cosine waves.

Method 2 – Adjust Figure Size After Creation

Sometimes, we create subplots first and then realize that the figure size is too small. Python allows us to fix this by using fig.set_size_inches().

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 20, 200)
y = np.sin(x)

# Create a subplot with default size
fig, ax = plt.subplots()
ax.plot(x, y, color='green')
ax.set_title("Adjust Size Later")

# Change figure size after creation
fig.set_size_inches(10, 6)

plt.show()

I executed the above example code and added the screenshot below.

Matplotlib Subplot Figure Size in Python

Here, I first created the subplot with the default size, then changed it to (10, 6). This is useful if you want flexibility in resizing after plotting.

Method 3 – Use Python’s plt.figure(figsize=…)

Another way to control subplot figure size in Python is by creating a figure first with plt.figure(figsize=…), and then adding subplots to it.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.linspace(0, 15, 150)
y1 = np.exp(-0.1 * x) * np.sin(x)
y2 = np.exp(-0.1 * x) * np.cos(x)

# Create figure with custom size
plt.figure(figsize=(14, 6))

# Add subplots manually
plt.subplot(1, 2, 1)
plt.plot(x, y1, color='purple')
plt.title("Damped Sine Wave")

plt.subplot(1, 2, 2)
plt.plot(x, y2, color='orange')
plt.title("Damped Cosine Wave")

plt.tight_layout()
plt.show()

This approach is handy when you want more manual control over subplot placement.

Python Matplotlib Subplot Figure Size

Method 4 – Combine subplots_adjust with Figure Size

Sometimes, even after setting the figure size, subplots can overlap. In that case, I use plt.subplots_adjust() along with figsize.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.linspace(0, 5, 100)
y1 = x**2
y2 = x**3
y3 = np.sqrt(x)

# Create subplots with figure size
fig, axes = plt.subplots(1, 3, figsize=(15, 5))

# Plot data
axes[0].plot(x, y1, color='blue')
axes[0].set_title("Square Function")

axes[1].plot(x, y2, color='red')
axes[1].set_title("Cubic Function")

axes[2].plot(x, y3, color='green')
axes[2].set_title("Square Root Function")

# Adjust spacing between subplots
plt.subplots_adjust(wspace=0.4)

plt.show()

I executed the above example code and added the screenshot below.

Subplot Figure Size Python Matplotlib

Here, I used wspace=0.4 to add more horizontal space between subplots. This makes the plots look cleaner.

Method 5 – Use constrained_layout=True in Python

Matplotlib also provides an option called constrained_layout that automatically adjusts subplot sizes and spacing.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.linspace(0, 2*np.pi, 200)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

# Create subplots with constrained layout
fig, axes = plt.subplots(3, 1, figsize=(8, 10), constrained_layout=True)

# Plot data
axes[0].plot(x, y1, color='blue')
axes[0].set_title("Sine Wave")

axes[1].plot(x, y2, color='red')
axes[1].set_title("Cosine Wave")

axes[2].plot(x, y3, color='green')
axes[2].set_title("Tangent Wave")

plt.show()

This method is great because I don’t have to manually adjust spacing. Python handles it automatically.

Method 6 – Use gridspec for Complex Layouts

For more advanced subplot figure size control, I sometimes use GridSpec. This allows me to create subplots of different sizes in the same figure.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

# Data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create figure with GridSpec
fig = plt.figure(figsize=(10, 8))
gs = gridspec.GridSpec(2, 2, figure=fig)

# Large plot
ax1 = fig.add_subplot(gs[0, :])
ax1.plot(x, y, color='blue')
ax1.set_title("Large Plot Across Top")

# Smaller plots
ax2 = fig.add_subplot(gs[1, 0])
ax2.plot(x, np.cos(x), color='red')
ax2.set_title("Bottom Left")

ax3 = fig.add_subplot(gs[1, 1])
ax3.plot(x, np.tan(x), color='green')
ax3.set_title("Bottom Right")

plt.tight_layout()
plt.show()

This approach is powerful when you need custom subplot arrangements.

Best Practices for Subplot Figure Size in Python

  • Always use figsize when creating subplots to avoid cramped plots.
  • Use tight_layout() or constrained_layout=True to fix overlapping labels.
  • For complex layouts, prefer GridSpec for better control.
  • Think about your audience: if your plots will be viewed on large screens, make them wider. If for printing, use balanced sizes.

Working with subplot figure sizes in Python can be tricky at first, but once you know the different methods, it becomes second nature.

I personally rely on figsize most of the time, but for professional reports, I also use constrained_layout or GridSpec.

With these techniques, you can make sure your Matplotlib plots always look clear, readable, and presentation-ready.

You may like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.