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.
flowchart LR
A["fig.savefig(path)"] --> B{"File extension"}
B -->|".png"| C["Raster image - web, slides"]
B -->|".pdf / .svg"| D["Vector image - print, infinite zoom"]
C --> E["dpi controls resolution"]
D --> F["Scales without pixelation"]
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
dpidpicontrols resolutionbbox_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 coffeeWas this page helpful?
Let us know how we did
