2025, Oct 18 06:00
Eliminate Horizontal Padding in Matplotlib QQ Plots Beside a Histogram with GridSpec and Zero-Width Columns
Learn how to remove horizontal whitespace in Matplotlib QQ plots beside a histogram using GridSpec, zero-width columns, and wspace=0 for tight readable layouts.
When you mix a histogram with a series of QQ plots in one row, the result often has awkward horizontal whitespace inside the QQ plot panels, even after you strip y-axis labels and ticks. Tweaking global spacing sometimes helps, but not always—especially when the subplots are heterogeneous in shape and content. Here is a clean way to make the QQ plots sit flush without the empty padding getting in the way.
Problem setup
The layout consists of a histogram followed by several QQ plots. Labels on the y-axis of the QQ plots are removed, and the y-axis is shared to keep scales aligned. Still, the QQ plots keep some horizontal padding that you may want to eliminate.
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
arr = np.random.normal(0, 1, 1000)
canvas, axe_row = plt.subplots(1, 4, figsize=(10, 4))
# First panel: histogram
axe_row[0].hist([1, 2, 3], bins=30, edgecolor='black')
axe_row[0].set_ylabel('Count')
axe_row[0].set_xlabel('Val')
# Remaining panels: QQ plots
for a in axe_row[1:]:
    stats.probplot(arr, dist="norm", plot=a)
# Remove y information on the QQ panels and attempt to share y
for a in axe_row[1:]:
    a.set_ylabel(None)
    a.tick_params(labelleft=False)
    a.sharey(axe_row[0])
canvas.tight_layout()
plt.show()
What causes the extra horizontal space
Global spacing tools affect gaps between axes, but they do not control the internal padding and margins each axes keeps for ticks, spines, and labels. After hiding y-axis labels and ticks, each QQ axes may still reserve room on the left, so simply tightening the layout or adjusting inter-axes spacing does not fully collapse that leftover area. When the first subplot is a histogram and the rest are QQ plots, those in-panel margins differ, and you get uneven or persistent whitespace.
Solution: GridSpec with width_ratios and zero-width separators
Use a GridSpec layout and control the widths explicitly. By inserting zero-width columns as separators and setting wspace to zero, you can visually eliminate the horizontal gap between the QQ plots while keeping the histogram sized appropriately.
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
arr = np.random.normal(0, 1, 1000)
fig_obj = plt.figure(figsize=(10, 4))
layout = fig_obj.add_gridspec(
    1, 7,
    width_ratios=[1.5, 0.5, 1, 0.00, 1, 0.00, 1],
    wspace=0
)
# Histogram
ax_histogram = fig_obj.add_subplot(layout[0])
ax_histogram.hist([1, 2, 3], bins=30, edgecolor='black')
ax_histogram.set_ylabel('Count')
ax_histogram.set_xlabel('Val')
# QQ plots: place them in columns 2, 4, 6 with zero-width spacers in between
qq_axes = [fig_obj.add_subplot(layout[i]) for i in range(2, 7, 2)]
stats.probplot(arr, dist="norm", plot=qq_axes[0])
qq_axes[0].set_ylabel('Y_label')
for ax in qq_axes[1:]:
    stats.probplot(arr, dist="norm", plot=ax)
    ax.set_ylabel(None)
    ax.set_yticklabels([])
    ax.set_yticks([])
    ax.spines['left'].set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()
This approach explicitly defines a seven-column grid: the histogram gets more width, the QQ plots are placed in alternating columns, and the columns in between have zero width. With wspace set to zero, the QQ plots end up tightly adjacent, removing the horizontal whitespace that persisted before.
Why this matters
Diagnostics become easier to read when subplots aren’t padded by unintended margins. In compact dashboards, publications, or quick EDA reports, every pixel of horizontal space counts. A predictable layout also makes it simpler to reason about alignments and to compare distributions across QQ plots.
Wrap-up
If you see stubborn gaps between QQ plots, think beyond global spacing controls and take charge of the grid. A GridSpec with width_ratios and wspace=0 lets you distribute width exactly where you want it, while zero-width columns act as invisible separators to eliminate internal whitespace. Hide the y-axis elements on the QQ panels, keep a consistent y-scale, and the row will read cleanly without wasted space.