Skip to content

Saving Plots as Images

Why saving matters

Saved plots are used in:

  • Reports
  • Notebooks
  • Websites
  • PowerPoint/Docs

McKinney’s original explanation is refreshingly simple: fig.savefig(path)fig.savefig(path) writes the active figure to disk, and matplotlib infers the file type from the extension you give it — .png.png for a raster image, .svg.svg/.pdf.pdf for vector formats that stay sharp at any zoom level.

diagram Choosing a save format mermaid
The file extension you pass to savefig() decides the output format and how it should be used.

Save to PNG

savefig
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Saved plot")
 
plt.savefig("plot.png", dpi=150, bbox_inches="tight")
savefig
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Saved plot")
 
plt.savefig("plot.png", dpi=150, bbox_inches="tight")

Notes

  • dpidpi controls resolution
  • bbox_inches="tight"bbox_inches="tight" avoids cutting off labels

Save to PDF (for print)

Save PDF
import matplotlib.pyplot as plt
 
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("PDF plot")
plt.savefig("plot.pdf", bbox_inches="tight")
Save PDF
import matplotlib.pyplot as plt
 
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("PDF plot")
plt.savefig("plot.pdf", bbox_inches="tight")

Next

You’ve covered every core chart type — continue to the Matplotlib Mini Project to combine them into one EDA charts pack.

🧪 Try It Yourself

Exercise 1 – Save a PNG at higher DPI

Exercise 2 – Infer format from the extension

Exercise 3 – Avoid clipped labels

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did