Saving Figures
savefig is the output side of every chart: raster (PNG, JPG) for screens and messaging, vector (SVG, PDF, EPS) for publications and infinite zoom. The method writes the Figure exactly as laid out - which makes two parameters worth knowing by heart: bbox_inches="tight" to clip the margin around the actual content, and dpi to set raster resolution. Everything else is format-specific detail.
Basic Saving
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4]
fig, ax = plt.subplots()
ax.plot(x, [v * v for v in x], marker="o")
fig.savefig("parabola.png") # raster, default 100 dpi
fig.savefig("parabola.svg") # vector
fig.savefig("parabola.pdf") # vector
plt.show()
The format comes from the file extension. The figure is saved in its current state - save before adding final annotations if you want the clean version, or just save the finished state. savefig does not close the figure; pair it with plt.close(fig) in loops and server code to free memory.
Tight Bounds and Resolution
fig.savefig("chart.png", dpi=200, bbox_inches="tight")
bbox_inches="tight"recomputes the saved bounding box to the content, dropping the default figure margins - the single most useful save parameter, because it fixes clipped legends and titles.dpi: screen targets want 100-150; print and zoom targets 200-300. A 6x4-inch figure at 200 dpi is a 1200x800 pixel image.facecolor/edgecolorset the canvas color;transparent=Truesaves PNG/SVG with no background - the standard for logos and overlay graphics.
Raster vs Vector
| Format | Kind | Use for |
|---|---|---|
| PNG | raster | web, docs, chat - the default |
| JPG | raster, lossy | photographic backgrounds; avoid for line art |
| SVG | vector | web embeds, infinite zoom, editable in Inkscape/Figma |
| vector | publications, slides, print - text stays selectable | |
| EPS | vector | legacy LaTeX toolchains |
Vector formats store geometry, not pixels: text stays crisp at any size and file size scales with complexity, not resolution. If the figure goes in a paper, a thesis, or a slide deck that gets zoomed, save SVG or PDF. If it goes in a dashboard or a message, PNG at 150+ dpi is fine.
Saving in Loops and Services
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
for i in range(5):
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [i, i + 1, i])
fig.savefig(f"series_{i}.svg", bbox_inches="tight")
plt.close(fig) # release the canvas - critical in long loops
The pattern: create, draw, save, close. In a web service that renders charts on demand, the Agg backend plus close is the entire rendering story - no display, no window, no accumulated memory. For very frequent small renders, fig.canvas.print_figure writes to a buffer you can stream directly to a response.
Summary
Save with the extension choosing the format, bbox_inches="tight" to fix margins, and dpi set for the destination. PNG for screens, SVG/PDF for anything printed or zoomed, transparent backgrounds for overlays, and plt.close after every save in non-interactive code. Ten seconds of parameter discipline and the output stops being an afterthought.