Skip to content

Saving Plots as Images

Saved plots are used in:

  • Reports
  • Notebooks
  • Websites
  • PowerPoint/Docs

McKinney’s original explanation is refreshingly simple: fig.savefig(path) writes the active figure to disk, and matplotlib infers the file type from the extension you give it — .png for a raster image, .svg/.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.
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")
  • dpi controls resolution
  • bbox_inches="tight" avoids cutting off labels
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")

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

Exercise 2 – Infer format from the extension

Section titled “Exercise 2 – Infer format from the extension”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading