Saving Plots as Images
Why saving matters
Section titled “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
Section titled “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")dpicontrols resolutionbbox_inches="tight"avoids cutting off labels
Save to PDF (for print)
Section titled “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")You’ve covered every core chart type — continue to the Matplotlib Mini Project to combine them into one EDA charts pack.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Save a PNG at higher DPI
Section titled “Exercise 1 – Save a PNG at higher DPI”Exercise 2 – Infer format from the extension
Section titled “Exercise 2 – Infer format from the extension”Exercise 3 – Avoid clipped labels
Section titled “Exercise 3 – Avoid clipped labels”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading