Python 如何将 Seaborn 图保存到文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32244753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save a Seaborn plot into a file
提问by neversaint
I tried the following code (test_seaborn.py
):
我尝试了以下代码(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:
我希望最终output.png
会存在,看起来像这样:
How can I resolve the problem?
我该如何解决问题?
采纳答案by Overclover
Remove the get_figure
and just use sns_plot.savefig('output.png')
删除get_figure
并使用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")
回答by Simon Gibbons
You should just be able to use the savefig
method of sns_plot
directly.
您应该可以直接使用 的savefig
方法sns_plot
。
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
为清楚起见,如果您确实想访问sns_plot
驻留在其中的 matplotlib 图,则可以直接使用
fig = sns_plot.fig
In this case there is no get_figure
method as your code assumes.
在这种情况下,没有get_figure
您的代码假定的方法。
回答by Salvatore Cosentino
The suggested solutions are incompatible with Seaborn 0.8.1
建议的解决方案与 Seaborn 0.8.1 不兼容
giving the following errors because the Seaborn interface has changed:
由于 Seaborn 界面已更改,因此出现以下错误:
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.8.1 compatible):
以下调用允许您访问图(Seaborn 0.8.1 兼容):
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
更新:我最近使用来自 seaborn 的 PairGrid 对象生成了一个类似于本例中的图。在这种情况下,由于 GridPlot 不是像 sns.swarmplot 那样的绘图对象,因此它没有 get_figure() 函数。可以通过以下方式直接访问 matplotlib 图
fig = myGridPlotObject.fig
Like previously suggested in other posts in this thread.
就像之前在此线程中的其他帖子中建议的那样。
回答by D.Mercer
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:
上述一些解决方案对我不起作用。.fig
尝试时未找到该属性,无法.savefig()
直接使用。然而,有效的是:
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.
我是一个新的 Python 用户,所以我不知道这是否是由于更新。我想提一下,以防其他人遇到和我一样的问题。
回答by Terry
I use distplot
and get_figure
to save picture successfully.
我使用distplot
和get_figure
成功保存图片。
sns_hist = sns.distplot(df_train['SalePrice'])
fig = sns_hist.get_figure()
fig.savefig('hist.png')
回答by mari?i
You would get an error for using sns.figure.savefig("output.png")
in seaborn 0.8.1.
sns.figure.savefig("output.png")
在 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")
回答by Sudhi
Just FYI, the below command worked in seaborn 0.8.1 so I guess the initial answer is still valid.
仅供参考,以下命令在 seaborn 0.8.1 中有效,所以我想最初的答案仍然有效。
sns_plot = sns.pairplot(data, hue='species', size=3)
sns_plot.savefig("output.png")
回答by tttthomasssss
Its also possible to just create a matplotlib figure
object and then use plt.savefig(...)
:
也可以只创建一个 matplotlibfigure
对象,然后使用plt.savefig(...)
:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
df = sns.load_dataset('iris')
plt.figure() # Push new figure on stack
sns_plot = sns.pairplot(df, hue='species', size=2.5)
plt.savefig('output.png') # Save that figure
回答by Niraj D Pandey
This works for me
这对我有用
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.factorplot(x='holiday',data=data,kind='count',size=5,aspect=1)
plt.savefig('holiday-vs-count.png')
回答by Jade Cacho
Fewer lines for 2019 searchers:
2019 年搜索者的行数减少:
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('iris')
sns_plot = sns.pairplot(df, hue='species', height=2.5)
plt.savefig('output.png')
UPDATE NOTE: size
was changed to height
.
更新注意:size
已更改为height
.