How to save a Seaborn plot into a file How to save a Seaborn plot into a file pandas pandas

How to save a Seaborn plot into a file


The suggested solutions are incompatible with Seaborn 0.8.1

giving the following errors because the Seaborn interface has changed:

AttributeError: 'AxesSubplot' object has no attribute 'fig'When trying to access the figureAttributeError: 'AxesSubplot' object has no attribute 'savefig'when trying to use the savefig directly as a function

The following calls allow you to access the figure (Seaborn 0.8.1 compatible):

swarm_plot = sns.swarmplot(...)fig = swarm_plot.get_figure()fig.savefig(...) 

as seen previously in this answer.

UPDATE:I have recently used PairGrid object from seaborn to generate a plot similar to the one in this example.In this case, since GridPlot is not a plot object like, for example, sns.swarmplot, it has no get_figure() function.It is possible to directly access the matplotlib figure by

fig = myGridPlotObject.fig

Like previously suggested in other posts in this thread.


This answer is for an old version of Seaborn, please see the newer answers below

The following is outdated: Remove the get_figure and just use sns_plot.savefig('output.png')

df = sns.load_dataset('iris')sns_plot = sns.pairplot(df, hue='species', height=2.5)sns_plot.savefig("output.png")


Some of the above solutions did not work for me. The .fig attribute was not found when I tried that and I was unable to use .savefig() directly. However, what did work was:

sns_plot.figure.savefig("output.png")

I am a newer Python user, so I do not know if this is due to an update. I wanted to mention it in case anybody else runs into the same issues as I did.