I tried the following code (test_seaborn.py
):
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
import seaborn as sns
sns.set()
df = sns.load_dataset('iris')
sns_plot = sns.pairplot(df, hue='species', size=2.5)
fig = sns_plot.get_figure()
fig.savefig("output.png")
#sns.plt.show()
But I get this error:
Traceback (most recent call last):
File "test_searborn.py", line 11, in <module>
fig = sns_plot.get_figure()
AttributeError: 'PairGrid' object has no attribute 'get_figure'
I expect the final output.png
will exist and look like this:
How can I resolve the problem?
Remove the
get_figure
and just usesns_plot.savefig('output.png')
I use
distplot
andget_figure
to save picture successfully.Its also possible to just create a matplotlib
figure
object and then useplt.savefig(...)
:You should just be able to use the
savefig
method ofsns_plot
directly.For clarity with your code if you did want to access the matplotlib figure that
sns_plot
resides in then you can get it directly withIn this case there is no
get_figure
method as your code assumes.Just FYI, the below command worked in seaborn 0.8.1 so I guess the initial answer is still valid.
You would get an error for using
sns.figure.savefig("output.png")
in seaborn 0.8.1.Instead use: