Saving Figures
Matplotlib is a Python library for creating charts and visualizations — and once a chart looks right, you'll want it as a file you can drop into a report, email, or web page.
Learn Saving Figures in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll use plt.savefig() to export charts as PNG, SVG, and PDF, and tune resolution, bounds, and transparency.
plt.savefig("name.png") writes the current figure to a file. The extension you choose decides the format — no extra settings needed.
What you'll see: a normal line chart on screen, and a new file my_chart.png in your working folder that contains the exact same image — open it and the chart is there.
Two arguments make saved charts look professional: dpi raises the resolution, and bbox_inches="tight" trims any wasted whitespace around the plot so labels aren't cut off.
What you'll see: the same on-screen chart, but the saved visitors.png is much higher resolution and cropped snugly around the plot and its labels, with no extra border.
Pass transparent=True to drop the white background — handy for slides and web pages. Save with a .svg or .pdf extension to get crisp vector output that scales to any size.
What you'll see: the purple chart on screen, plus three files — a PNG whose background is see-through, and SVG and PDF versions that stay razor-sharp no matter how far you zoom in.
Replace each ___ to save a high-resolution, neatly trimmed chart.
You called savefig() after show() . The figure was already cleared. Save first.
Add bbox_inches="tight" so Matplotlib expands the bounds to include every label.
Build a labelled chart and export it twice — a print PNG and a vector PDF.
Lesson 6 complete — your charts are export-ready!
You saved figures as PNG, SVG, and PDF, tuned resolution with dpi, trimmed bounds, and added transparency.
🚀 Up next: Bar Charts — move beyond lines to compare categories side by side.
Practice quiz
Which call writes the current figure to a file?
- plt.export()
- plt.write()
- plt.savefig('chart.png')
- plt.dump()
Answer: plt.savefig('chart.png'). plt.savefig('chart.png') saves the current figure to disk.
How does savefig decide the file format?
- From the file extension
- From a format=auto flag
- It always saves PNG
- From the dpi value
Answer: From the file extension. The extension (.png, .svg, .pdf) selects the output format.
What does the dpi argument control?
- The figure title
- The resolution of raster output
- The background color
- The line style
Answer: The resolution of raster output. dpi (dots per inch) sets the resolution; dpi=300 is common for print.
Which argument trims whitespace so labels are not cut off?
- pad='none'
- trim=True
- crop='auto'
- bbox_inches='tight'
Answer: bbox_inches='tight'. bbox_inches='tight' crops the image snugly around the plot and labels.
How do you save with a see-through background?
- transparent=True
- background=None
- alpha=0
- clear=True
Answer: transparent=True. Pass transparent=True to drop the white background (PNG/SVG).
Why must savefig come before show()?
- show() runs faster afterward
- show() clears the figure, leaving a blank save
- savefig needs the window open
- Order does not matter
Answer: show() clears the figure, leaving a blank save. After show() the figure is cleared, so a later savefig writes a blank image.
Which extensions give crisp vector output?
- .jpg and .gif
- .png and .bmp
- .svg and .pdf
- .tif and .jpg
Answer: .svg and .pdf. .svg and .pdf are vector formats that scale without blurring.
Which format ignores transparent=True?
- PNG
- SVG
- JPG
Answer: JPG. JPEG has no alpha channel, so transparent=True is ignored for JPG.
What dpi value is the usual standard for print?
- 72
- 100
- 300
- 10
Answer: 300. dpi=300 is the usual print standard; default 100 is fine for the web.
Why might a saved file appear blank or empty?
- dpi was too high
- The extension was .png
- savefig was called after show()
- bbox_inches was used
Answer: savefig was called after show(). Calling savefig after show() saves an already-cleared figure.