Lecture

Saving Plots to File

Sometimes, you’ll need to save your plots for later use — whether to include them in reports, presentations, or share them with others.

Matplotlib makes this simple with the savefig() function.

Note: In this notebook, savefig() is shown for demonstration only. To actually download and view the image file, run the same code in your local Python environment.


Saving the Current Plot

Use plt.savefig("filename.ext") to export the current plot.

Saving as PNG
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] plt.plot(x, y) plt.title("Trend Line") plt.savefig("plot.png") # Saves the image file

You can use various formats: "png", "jpg", "svg", "pdf", etc.

Make sure to call savefig() before plt.show() — otherwise, the saved image might be empty.


Controlling Image Quality

You can adjust image resolution using the dpi (dots per inch) parameter.

High Resolution Export
plt.savefig("plot_high_res.png", dpi=300)

This is especially useful when preparing figures for reports, slides, or publications where higher clarity is needed.


Saving Without Displaying

You can save a plot without calling plt.show(). This is helpful when generating many charts automatically.

Save Without Showing
plt.plot(x, y) plt.title("Autosave Example") plt.savefig("autosave.png")

The plot won’t appear on screen, but it will still be saved to your working directory.

Quiz
0 / 1

What function in Matplotlib is used to save plots directly to an image file?

plt.show()

plt.plot()

plt.savefig()

plt.title()

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help