Save a Matplotlib Plot as PNG Without Borders in Python

I was working on a Python project where I had to generate hundreds of visual charts using Matplotlib for a data dashboard. The challenge? Every saved PNG image had unwanted white borders around it.

If you’ve ever tried saving a Matplotlib figure as a PNG, you might have noticed those pesky white spaces or frames around the image. It doesn’t look clean, especially when you’re embedding charts into web apps, reports, or dashboards.

After spending hours experimenting and reading documentation, I found a few simple and effective ways to save a Matplotlib PNG image without any border. In this tutorial, I’ll share these methods step-by-step, the same ones I use in my professional Python projects.

Why Does Matplotlib Add Borders by Default?

When you create a figure in Matplotlib, it automatically includes padding, axes, labels, and margins to make the plot readable.
While this is great for analysis, it becomes a problem when you need a clean, borderless image for design, reports, or web integration.

The good news? You can easily control how much space gets saved around your figure using a few parameters in the savefig() function.

Method 1 – Use bbox_inches=’tight’ and pad_inches=0 in Matplotlib

This is the simplest and most commonly used method to remove extra white spaces or borders from your saved Matplotlib figure.

Here’s how I usually do it in my Python projects.

Step-by-Step Example

First, let’s create a simple line chart and save it without any border.

import matplotlib.pyplot as plt
import numpy as np

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

# Create a figure and axis
plt.figure(figsize=(6, 4))
plt.plot(x, y, color='blue', linewidth=2)
plt.title("Sine Wave - Matplotlib Example", fontsize=14)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")

# Save the figure without borders
plt.savefig("sine_wave_no_border.png", bbox_inches='tight', pad_inches=0)
plt.show()

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

Save a Matplotlib Plot as PNG Without Borders Python

In the code above, I used:

  • bbox_inches=’tight’ → This tells Matplotlib to fit the figure tightly around the content.
  • pad_inches=0 → This removes any additional padding around the figure.

When you open the saved image, you’ll see that there’s no extra white space or border, just your chart. This is my go-to method when exporting charts for reports or when embedding them into a dashboard UI.

Method 2 – Remove Axes and Frame Before Saving the PNG

Sometimes, you may not just want to remove white spaces; you might also want to hide the axes and frame altogether. This is useful when you’re creating visual elements like icons, thumbnails, or visual components for a web app.

Here’s how I do it.

import matplotlib.pyplot as plt
import numpy as np

# Generate data
x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)

# Create a figure
fig, ax = plt.subplots(figsize=(5, 3))

# Plot data
ax.plot(x, y, color='green', linewidth=3)

# Remove axes and frame
ax.axis('off')

# Save without borders
plt.savefig("sine_wave_clean.png", bbox_inches='tight', pad_inches=0, transparent=True)
plt.show()

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

Save Matplotlib Plot as PNG Without Borders Python

In this example, I used ax.axis(‘off’) to hide the axes and ticks completely. Then, I saved the figure with the transparent=True parameter, which makes the background transparent, perfect for overlaying on colored backgrounds or web pages.

Method 3 – Create a Borderless Figure Using Custom Axes Position

Another advanced technique I often use is to manually control how the axes fill the figure. By setting the axes position to fill the entire figure area, you can completely remove any unwanted margins.

Here’s how it works.

import matplotlib.pyplot as plt
import numpy as np

# Data for a bar chart
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 12, 36]

# Create a borderless figure
fig = plt.figure(frameon=False)
fig.set_size_inches(4, 3)

# Add axes that fill the entire figure
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)

# Plot the bar chart
ax.bar(categories, values, color='orange')

# Save as PNG without borders
fig.savefig("bar_chart_no_border.png", dpi=200)
plt.show()

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

Save Python Matplotlib Plot as PNG Without Borders

In this code:

  • I created the figure with frameon=False to remove the outer frame.
  • Then, I added custom axes using plt.Axes(fig, [0., 0., 1., 1.]) to make the plot fill the entire figure.
  • Finally, I saved it using savefig(), no borders, no padding, no frame.

This technique gives you pixel-perfect control and is ideal for automated image generation in Python scripts.

Method 4 – Use tight_layout() Before Saving

Sometimes, even after using bbox_inches=’tight’, you might notice a tiny border around your figure. This happens because of the subplot or label spacing. In such cases, I use the plt.tight_layout() function before saving.

Here’s how:

import matplotlib.pyplot as plt
import numpy as np

# Create data
x = np.linspace(0, 5, 50)
y1 = np.sin(x)
y2 = np.cos(x)

# Create subplots
fig, ax = plt.subplots(2, 1, figsize=(6, 6))

# Plot data
ax[0].plot(x, y1, color='red', label='Sine')
ax[1].plot(x, y2, color='blue', label='Cosine')

# Add titles and labels
ax[0].set_title('Sine Function')
ax[1].set_title('Cosine Function')
for a in ax:
    a.legend()

# Adjust layout and save
plt.tight_layout()
plt.savefig("trig_functions_no_border.png", bbox_inches='tight', pad_inches=0)
plt.show()

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

Save Matplotlib Plot as PNG Without Borders

Using tight_layout() automatically adjusts subplot parameters to remove unnecessary padding, ensuring your saved PNG looks clean and professional.

Method 5 – Transparent PNG Without Borders in Python

If you’re building a web application or dashboard in Python, you might need transparent PNGs. This helps when you want your charts to blend seamlessly with any background color.

Here’s how you can do that in Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

# Generate data
x = np.linspace(0, 2 * np.pi, 200)
y = np.cos(x)

# Create the plot
plt.plot(x, y, color='purple', linewidth=2)
plt.title("Cosine Wave - Transparent PNG")

# Save as transparent PNG without borders
plt.savefig("cosine_wave_transparent.png", bbox_inches='tight', pad_inches=0, transparent=True)
plt.show()

Notice the use of transparent=True, which makes the background completely transparent.
This is perfect for embedding your Python-generated charts into presentations, websites, or dashboards.

Common Mistakes to Avoid

Over the years, I’ve seen some common mistakes developers make when saving borderless figures in Matplotlib:

  1. Forgetting pad_inches=0 – Even with bbox_inches=’tight’, you’ll still see small borders if you skip this.
  2. Not calling plt.show() – Always show or close figures properly, especially in batch scripts.
  3. Using low DPI – For professional results, always save with higher DPI (dpi=150 or dpi=300).
  4. Not disabling axes when needed – If you want a truly clean image, use ax.axis(‘off’).

Avoiding these small mistakes will ensure you always get crisp, borderless PNGs from your Python scripts.

Bonus Tip – Automate Borderless Saving for Multiple Figures

If you generate many plots in a loop, you can automate borderless saving using a small helper function.

import matplotlib.pyplot as plt

def save_clean_figure(fig, filename):
    fig.savefig(filename, bbox_inches='tight', pad_inches=0, transparent=True, dpi=200)
    plt.close(fig)

# Example usage
for i in range(3):
    fig, ax = plt.subplots()
    ax.plot([0, 1, 2], [i, i+1, i+2], linewidth=2)
    save_clean_figure(fig, f"plot_{i}.png")

This approach is particularly helpful when generating multiple figures dynamically in Python, such as in data pipelines or reporting systems.

So that’s how I save Matplotlib PNG images without borders in Python. Whether you’re preparing visuals for a presentation, report, or web dashboard, these methods will give you clean, professional-looking charts every time.

To summarize:

  • Use bbox_inches=’tight’ and pad_inches=0 for quick fixes.
  • Remove axes and frames for minimalist designs.
  • Combine tight_layout() for subplot adjustments.
  • Use transparent=True for overlay-friendly PNGs.

These techniques have saved me countless hours in my work with Python and Matplotlib. Once you start using them, you’ll never have to crop borders manually again!

You may also 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.