Table of Contents
Saving a Matplotlib figure to a file refers to the process of exporting a visual representation of data created using the Matplotlib library to a permanent file on a computer. This allows for the figure to be shared, printed, or used for reference in the future. To save a Matplotlib figure, one must use the savefig() function, which allows for the selection of the desired file format and location. This function is a convenient and essential tool when working with data visualization in Matplotlib.
Save Matplotlib Figure to a File (With Examples)
You can use the following basic syntax to save a Matplotlib figure to a file:
import matplotlib.pyplotas plt #save figure in various formats plt.savefig('my_plot.png') plt.savefig('my_plot.jpg') plt.savefig('my_plot.pdf')
The following examples show how to use this syntax in practice.
Example 1: Save Matplotlib Figure to PNG File
The following code shows how to save a Matplotlib figure to a PNG file:
import matplotlib.pyplotas plt #define data x = [1, 2, 3, 4, 5, 6] y = [8, 13, 14, 11, 16, 22] #create scatterplot with axis labels plt.plot(x, y) plt.xlabel('X Variable') plt.ylabel('Y Variable') #save figure to PNG file plt.savefig('my_plot.png')
If we navigate to the location where we saved the file, we can view it:

Example 2: Save Matplotlib Figure with Tight Layout
By default, Matplotlib adds generous padding around the outside of the figure.
To remove this padding, we can use the bbox_inches=’tight’ argument:
#save figure to PNG file with no padding plt.savefig('my_plot.png', bbox_inches='tight')

Notice that there is less padding around the outside of the plot.
Example 3: Save Matplotlib Figure with Custom Size
You can also use the dpi argument to increase the size of the Matplotlib figure when saving it:
#save figure to PNG file with increased size plt.savefig('my_plot.png', dpi = 100)

You can find the complete online documentation for the Matplotlib savefig() function .
The following tutorials explain how to perform other common functions in Matplotlib:
Cite this article
stats writer (2024). How can I save a Matplotlib figure to a file?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-save-a-matplotlib-figure-to-a-file/
stats writer. "How can I save a Matplotlib figure to a file?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-save-a-matplotlib-figure-to-a-file/.
stats writer. "How can I save a Matplotlib figure to a file?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-save-a-matplotlib-figure-to-a-file/.
stats writer (2024) 'How can I save a Matplotlib figure to a file?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-save-a-matplotlib-figure-to-a-file/.
[1] stats writer, "How can I save a Matplotlib figure to a file?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I save a Matplotlib figure to a file?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
