Why saving matters
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.
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
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
dpicontrols resolutionbbox_inches="tight"avoids cutting off labels
Save to PDF (for print)
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("PDF plot")
plt.savefig("plot.pdf", bbox_inches="tight")Check your understanding
How does Matplotlib decide the file type when you call savefig?
plot.png writes a raster image, while plot.svg or plot.pdf write vector formats.
Show answer
A — From the file extension you give it — plot.png writes a raster image, while plot.svg or plot.pdf write vector formats.
What does dpi=150 control in plt.savefig("plot.png", dpi=150)?
dpi controls resolution, so a higher value gives a sharper raster image.
Show answer
A — The resolution of the image — dpi controls resolution, so a higher value gives a sharper raster image.
Why pass bbox_inches="tight"?
Tight bounding boxes crop around the artists and keep axis labels and titles inside the saved image.
Show answer
A — It avoids cutting off labels — Tight bounding boxes crop around the artists and keep axis labels and titles inside the saved image.
Which formats stay sharp at any zoom level?
Vector formats store shapes instead of pixels, so they scale without blurring.
Show answer
A — SVG and PDF vector formats — Vector formats store shapes instead of pixels, so they scale without blurring.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Infer format from the extension
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Avoid clipped labels
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.