[英]How to save a Seaborn plot into a file
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?我该如何解决这个问题?
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 .更新:我最近使用来自 seaborn 的 PairGrid 对象生成了一个类似于本例中的图。 In this case, since GridPlot is not a plot object like, for example, sns.swarmplot, it has no get_figure() function.
在这种情况下,由于 GridPlot 不是像 sns.swarmplot 那样的绘图对象,因此它没有 get_figure() 函数。 It is possible to directly access the matplotlib figure by
可以通过以下方式直接访问 matplotlib 图
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此答案适用于旧版本的 Seaborn,请参阅下面的较新答案
The following is outdated : 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', 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.尝试时未找到
.fig
属性,并且无法直接使用.savefig()
。 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.我是一个新的 Python 用户,所以我不知道这是否是由于更新。 I wanted to mention it in case anybody else runs into the same issues as I did.
我想提一下,以防其他人遇到和我一样的问题。
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
。
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
方法。
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')
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')
Its also possible to just create a matplotlib figure
object and then use plt.savefig(...)
:也可以只创建一个 matplotlib
figure
对象,然后使用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
You would get an error for using sns.figure.savefig("output.png")
in seaborn 0.8.1.在 seaborn 0.8.1 中使用
sns.figure.savefig("output.png")
会出错。
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")
I couldnt get the other answers to work and finally got this to work for me for matplotlib==3.2.1 .我无法得到其他答案的工作,并最终得到它为我工作 matplotlib==3.2.1 。 Its especially true if you are doing this within a for loop or some iterative approach.
如果您在 for 循环或某种迭代方法中执行此操作,则尤其如此。
sns.scatterplot(
data=df_hourly, x="date_week", y="value",hue='variable'
)
plt.savefig('./f1.png')
plt.show()
Note that the savefig must be before the show call.请注意, savefig 必须在 show 调用之前。 Otherwise an empty image is saved.
否则将保存一个空图像。
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.