matplotlib savefig: dpi, size, transparent, blank image (tested)
Saved pixel size = figsize x dpi (default dpi 100). bbox_inches="tight" crops, transparent=True clears the background, and a blank file means plt.savefig ran after the figure was closed.
fig.savefig() options measured with matplotlib 3.11.2 (Agg backend) and Pillow on Python 3.13.5. Pixel sizes and colours below were read back from the saved files.
1. matplotlib savefig dpi and size: pixels = figsize x dpi
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PIL import Image
fig, ax = plt.subplots(figsize=(4, 3))
ax.plot([1, 2, 3], [1, 4, 9])
for dpi in (None, 100, 300):
fig.savefig(f"plot_{dpi}.png", dpi=dpi)
print(f"dpi={dpi}: {Image.open(f'plot_{dpi}.png').size} pixels")
print("rcParams savefig.dpi:", matplotlib.rcParams["savefig.dpi"], "figure.dpi:", matplotlib.rcParams["figure.dpi"])
Output:
dpi=None: (400, 300) pixels
dpi=100: (400, 300) pixels
dpi=300: (1200, 900) pixels
rcParams savefig.dpi: figure figure.dpi: 100.0
The default savefig.dpi is "figure", i.e. the figure dpi (100), so a 4x3 inch figure saves as 400x300. For a high-resolution PNG raise dpi; to change the shape, change figsize.
2. bbox_inches="tight" and transparent background
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PIL import Image
fig, ax = plt.subplots(figsize=(4, 3))
ax.plot([1, 2, 3], [1, 4, 9])
ax.set_ylabel("a long y label that sticks out")
fig.savefig("normal.png", dpi=100)
fig.savefig("tight.png", dpi=100, bbox_inches="tight")
fig.savefig("transparent.png", dpi=100, transparent=True)
for f in ("normal.png", "tight.png", "transparent.png"):
im = Image.open(f).convert("RGBA")
print(f"{f:16} size={im.size} corner pixel RGBA={im.getpixel((0, 0))}")
Output:
normal.png size=(400, 300) corner pixel RGBA=(255, 255, 255, 255)
tight.png size=(368, 274) corner pixel RGBA=(255, 255, 255, 255)
transparent.png size=(400, 300) corner pixel RGBA=(255, 255, 255, 0)
bbox_inches="tight" crops the saved image to the drawn content, so the pixel size no longer equals figsize x dpi. transparent=True makes the figure background fully transparent (alpha 0) in PNG.
3. File format comes from the extension
import os, matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
for name in ("out.pdf", "out.svg", "out"):
fig.savefig(name)
print(name, "->", open(name if os.path.exists(name) else name + ".png", "rb").read(5))
print("files:", sorted(f for f in os.listdir() if f.startswith("out")))
try:
fig.savefig("out.jpeg2")
except ValueError as e:
print("ValueError:", str(e)[:90])
Output:
out.pdf -> b'%PDF-'
out.svg -> b'<?xml'
out -> b'\x89PNG\r'
files: ['out.pdf', 'out.png', 'out.svg']
ValueError: Format 'jpeg2' is not supported (supported formats: avif, eps, gif, jpeg, jpg, pdf, pgf, p
No extension means PNG (savefig.format), and "out" becomes out.png. An unknown extension raises ValueError listing the supported formats. Use format="png" to override.
4. matplotlib savefig blank image
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PIL import Image
plt.plot([1, 2, 3])
plt.close()
plt.savefig("after_close.png")
print("savefig after plt.close(): min/max gray =", Image.open("after_close.png").convert("L").getextrema())
plt.plot([1, 2, 3])
plt.savefig("before_close.png")
plt.close()
print("savefig before plt.close(): min/max gray =", Image.open("before_close.png").convert("L").getextrema())
Output:
savefig after plt.close(): min/max gray = (255, 255)
savefig before plt.close(): min/max gray = (0, 255)
After plt.close(), plt.savefig() saves a new empty figure: every pixel is white. The same symptom is commonly reported when savefig is called after plt.show() in an interactive window (not tested here). Call savefig before show()/close(), or keep the figure object and call fig.savefig().
Related: matplotlib not showing plot, matplotlib suppress warnings, numpy RuntimeWarning.
Frequently asked questions
How do I save a high resolution figure in matplotlib?
fig.savefig('plot.png', dpi=300). A 4x3 inch figure became 1200x900 pixels in the test.
Why is my matplotlib savefig image blank?
In the test, plt.savefig() after plt.close() saved a new, all-white figure. Call savefig before show()/close(), or use fig.savefig().
How do I save a matplotlib figure with a transparent background?
fig.savefig('plot.png', transparent=True); the background pixels had alpha 0 in the test.
Written with AI assistance. Measured on 2026-10-23 with matplotlib 3.11.2 (Agg backend) and Pillow on Python 3.13.5. Each block was run verbatim and the output pasted unedited. Interactive backends were not tested. PlainTables is our extension; Python and matplotlib are free.