Matplotlib subplots_adjust

One of the libraries I keep coming back to for data visualization is Matplotlib. It’s flexible and widely used across industries in the USA, from finance to healthcare. But if you’ve ever created multiple plots in the same figure, you might have noticed how tricky it can be to get the spacing just right.

That’s where subplots_adjust comes in. This handy Matplotlib function lets you control the spacing between your subplots and the figure edges, making your visualizations look clean and professional.

In this article, I’ll share my firsthand experience using subplots_adjust to help you fine-tune your plots effortlessly.

What is Matplotlib subplots_adjust?

When you create multiple plots using Matplotlib’s subplots or subplot functions, the default spacing might leave your plots cramped or too spread out. subplots_adjust allows you to manually tweak the margins and spacing between the plots inside a figure.

From my experience, this is essential when preparing reports or dashboards for stakeholders who expect polished, easy-to-read visuals, especially in data-driven environments like marketing analytics or public health reporting in the US.

How to Use subplots_adjust: Basic Syntax

The function is called on the figure object like this:

fig.subplots_adjust(left=None, right=None, bottom=None, top=None, wspace=None, hspace=None)
  • left, right, bottom, top: Control the position of the subplot edges as a fraction of the figure width and height.
  • wspace: Width space between subplots (fraction of average subplot width).
  • hspace: Height space between subplots (fraction of average subplot height).

You can pass any combination of these parameters to adjust your layout.

Method 1: Adjust Margins Around Subplots

Often, your plots might be too close to the figure edges. For example, if you’re plotting quarterly sales data for New York and California side by side, you want some breathing room.

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 2)
axs[0].plot([100, 200, 300, 400], label='NY Sales Q1-Q4')
axs[1].plot([150, 250, 350, 450], label='CA Sales Q1-Q4')

fig.subplots_adjust(left=0.1, right=0.9, bottom=0.2, top=0.8)
plt.show()

You can refer to the screenshot below to see the output.

subplots_adjust

Here, I set the left margin to 10% and the right margin to 90% of the figure width, giving the plots enough space to breathe without wasting canvas area.

Read Matplotlib is currently using agg, a non-GUI backend

Method 2: Control Space Between Subplots Horizontally and Vertically

When you have multiple rows and columns of plots say, visualizing unemployment rates across 4 US states, controlling the space between them is crucial.

fig, axs = plt.subplots(2, 2)

# Sample data plotting here...

fig.subplots_adjust(wspace=0.3, hspace=0.4)
plt.show()

You can refer to the screenshot below to see the output.

plt.subplots_adjust

From my experience, tweaking wspace and hspace helps prevent overlapping axis labels and titles, making charts easier to read.

Method 3: Use Tight Layout vs. subplots_adjust

Matplotlib also offers plt.tight_layout() that automatically adjusts subplot parameters for you.

fig, axs = plt.subplots(2, 2)
# plot code here
plt.tight_layout()
plt.show()

You can refer to the screenshot below to see the output.

matplotlib subplots_adjust

While tight_layout is great for quick fixes, I’ve found it sometimes doesn’t perfectly handle complex layouts or custom annotations. subplots_adjust gives you the fine control needed for professional presentations or publications.

Check out Matplotlib Time Series Plot

Method 4: Combine subplots_adjust with Figure Size

Adjusting subplot spacing alone isn’t enough if your figure size is too small or too large. I usually specify figure size explicitly:

fig, axs = plt.subplots(2, 2, figsize=(12, 8))
fig.subplots_adjust(left=0.08, right=0.95, bottom=0.1, top=0.9, wspace=0.25, hspace=0.35)
plt.show()

This combination ensures your plots have the right amount of space both inside and outside the subplots.

Practical Example: Visualize COVID-19 Trends Across US States

Let’s say you want to compare COVID-19 case counts in New York, California, Texas, and Florida over time. You create a 2×2 grid of line charts.

Without adjusting spacing, labels and titles might overlap, making it hard for public health officials to interpret the data.

By applying subplots_adjust, you can create a clean dashboard that fits all plots neatly:

fig, axs = plt.subplots(2, 2, figsize=(14, 10))

states = ['New York', 'California', 'Texas', 'Florida']
for ax, state in zip(axs.flatten(), states):
    # Assume covid_data[state] contains time series data
    ax.plot(covid_data[state]['date'], covid_data[state]['cases'])
    ax.set_title(f'COVID-19 Cases in {state}')
    ax.set_xlabel('Date')
    ax.set_ylabel('Cases')

fig.subplots_adjust(left=0.07, right=0.95, bottom=0.1, top=0.9, wspace=0.3, hspace=0.4)
plt.show()

This approach ensures the dashboard is reader-friendly and visually balanced.

Read Module ‘matplotlib’ has no attribute ‘artist’

Tips from My Experience

  • Always start with a figure size that suits your output medium (screen, presentation, print).
  • Use wspace and hspace to avoid label and title collisions.
  • Adjust margins (left, right, top, bottom) to prevent clipping of axis labels.
  • When in doubt, combine tight_layout() with subplots_adjust for best results.
  • Save your figures in high resolution for presentations or reports.

Mastering subplots_adjust has made a huge difference in how I present data. It’s a small but powerful tool that elevates your Matplotlib plots from cluttered to clean and professional. Whether you’re preparing quarterly sales reports or public health dashboards, fine-tuning your subplot layout will impress your audience and make your insights clearer.

If you want to dive deeper, I recommend experimenting with different parameter values to see how your plots respond. With a little practice, you’ll have full control over your figure layouts and produce stunning visualizations every time.

You may like to read:

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.