How to save a Seaborn plot into a file

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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      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?

回答1:

Remove the get_figure and just use sns_plot.savefig('output.png')

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


回答2:

The suggested solutions are incompatible with Seaborn 0.7.1

giving the following errors because the Seaborn interface has changed:

AttributeError: 'AxesSubplot' object has no attribute 'fig' When trying to access the figure  AttributeError: '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.7.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.



回答3:

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.



回答4:

You should just be able to use the savefig method of sns_plot directly.

sns_plot.savefig("output.png") 

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 with

fig = sns_plot.fig 

In this case there is no get_figure method as your code assumes.



回答5:

I use distplot and get_figure to save picture successfully.

sns_hist = sns.distplot(df_train['SalePrice']) fig = sns_hist.get_figure() fig.savefig('hist.png') 


回答6:

You would get an error for using sns.figure.savefig("output.png") in seaborn 0.8.1.

Instead use:

import seaborn as sns  df = sns.load_dataset('iris') sns_plot = sns.pairplot(df, hue='species', size=2.5) sns_plot.savefig("output.png") 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!