Python Central HubSign in

Data Analytics with Python / Phase 5: Data Visualization with Matplotlib

Saving Plots as Images

Export plots with dpi and tight bounding boxes for reports and websites.

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.

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

Python
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

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

Save to PDF (for print)

Python
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

quizCheck yourself
  1. How does Matplotlib decide the file type when you call savefig?

    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.

  2. What does dpi=150 control in plt.savefig("plot.png", dpi=150)?

    Show answer

    A — The resolution of the image — dpi controls resolution, so a higher value gives a sharper raster image.

  3. Why pass bbox_inches="tight"?

    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.

  4. Which formats stay sharp at any zoom level?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Infer format from the extension

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Avoid clipped labels

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did