Table of Contents
To save a Seaborn plot to a file, you can use the “savefig” function from the matplotlib library. This function allows you to specify the file name and format in which you want to save the plot. For example, to save a Seaborn plot named “scatterplot” as a PNG file, you can use the following code:
plt.savefig(‘scatterplot.png’)
Alternatively, you can also use the “fig.savefig” method from the Seaborn library itself. This method also allows you to specify the file name and format. For example:
fig.savefig(‘scatterplot.png’)
Both of these methods will save the plot in the same directory as your current working directory. You can also specify a full path for the file if you want to save it in a specific location.
Save Seaborn Plot to a File (With Examples)
You can use the following syntax to save a Seaborn plot to a file:
import seaborn as sns line_plot = sns.lineplot(x=x, y=y) fig = line_plot.get_figure() fig.savefig('my_lineplot.png')
The following examples show how to use this syntax in practice.
Example 1: Save Seaborn Plot to PNG File
The following code shows how to save a Seaborn plot to a PNG file:
import seaborn as sns #set theme style sns.set_style('darkgrid') #define data x = [1, 2, 3, 4, 5, 6] y = [8, 13, 14, 11, 16, 22] #create line plot and save as PNG file line_plot = sns.lineplot(x=x, y=y) fig = line_plot.get_figure() fig.savefig('my_lineplot.png')
If we navigate to the location where we saved the file, we can view it:

Note that we could also use .jpg, .pdf, or other file extensions to save the plot to a different type of file.
Example 2: Save Seaborn Plot to PNG File with Tight Layout
By default, Seaborn adds padding around the outside of the figure.
To remove this padding, we can use the bbox_inches=’tight’ argument:
fig.savefig('my_lineplot.png', bbox_inches='tight')

Notice that there is minimal padding around the outside of the plot now.
Example 3: Save Seaborn Plot to PNG File with Custom Size
You can use the dpi argument to increase the size of the Seaborn plot when saving it to a file:
fig.savefig('my_lineplot.png', dpi=100)

Notice that this plot is much larger than the previous two. The larger the value you use for dpi, the larger the plot will be.
The following tutorials explain how to perform other common plotting functions in Seaborn:
Cite this article
stats writer (2024). How can I save a Seaborn plot to a file? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-save-a-seaborn-plot-to-a-file-can-you-provide-some-examples/
stats writer. "How can I save a Seaborn plot to a file? Can you provide some examples?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-save-a-seaborn-plot-to-a-file-can-you-provide-some-examples/.
stats writer. "How can I save a Seaborn plot to a file? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-save-a-seaborn-plot-to-a-file-can-you-provide-some-examples/.
stats writer (2024) 'How can I save a Seaborn plot to a file? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-save-a-seaborn-plot-to-a-file-can-you-provide-some-examples/.
[1] stats writer, "How can I save a Seaborn plot to a file? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I save a Seaborn plot to a file? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
