How to remove whitespaces at the bottom of a Matplotlib graph?

When creating Matplotlib plots, you may notice unwanted whitespace at the bottom or around your graph. Python provides several methods to remove this whitespace and create cleaner, more professional-looking plots.

Steps to Remove Whitespace

  • Set the figure size and adjust the padding between and around the subplots

  • Create a new figure or activate an existing figure

  • Add an subplot to the figure with proper scaling parameters

  • Plot your data points using the plot() method

  • Apply whitespace removal techniques like tight_layout() or autoscale_on=False

  • Display the figure using show() method

Method 1: Using autoscale_on=False

This method gives you precise control over the plot limits, eliminating unwanted whitespace ?

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False, xlim=(0, 5), ylim=(0, 7))

data = [2, 5, 1, 2, 0, 7]
ax.plot(data)

plt.show()

Method 2: Using tight_layout()

The tight_layout() method automatically adjusts subplot parameters to give specified padding ?

import matplotlib.pyplot as plt

plt.figure(figsize=(7.50, 3.50))

data = [2, 5, 1, 2, 0, 7]
plt.plot(data)

plt.tight_layout(pad=0.1)
plt.show()

Method 3: Using subplots_adjust()

For more granular control, use subplots_adjust() to manually set margins ?

import matplotlib.pyplot as plt

plt.figure(figsize=(7.50, 3.50))

data = [2, 5, 1, 2, 0, 7]
plt.plot(data)

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

Comparison of Methods

Method Best For Control Level
autoscale_on=False Precise axis limits High
tight_layout() Automatic adjustment Low
subplots_adjust() Manual margin control High

Key Parameters

  • xlim, ylim: Set explicit axis limits to control data range

  • pad: Padding around the plot in tight_layout()

  • bottom, top, left, right: Margin values in subplots_adjust()

Conclusion

Use tight_layout() for quick automatic adjustment, autoscale_on=False for precise axis control, or subplots_adjust() for manual margin customization. Each method effectively removes unwanted whitespace from your Matplotlib plots.

---
Updated on: 2026-03-26T00:32:26+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements