繁体   English   中英

如何将 Seaborn 图保存到文件中

[英]How to save a Seaborn plot into a file

我尝试了以下代码( 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()

但我得到这个错误:

  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'

我希望最终的output.png会存在并且看起来像这样:

在此处输入图像描述

我该如何解决这个问题?

建议的解决方案与 Seaborn 0.8.1 不兼容

由于 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

以下调用允许您访问图(Seaborn 0.8.1 兼容):

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

如之前在此答案中所见。

更新:我最近使用来自 seaborn 的 PairGrid 对象生成了一个类似于本例中的图 在这种情况下,由于 GridPlot 不是像 sns.swarmplot 那样的绘图对象,因此它没有 get_figure() 函数。 可以通过以下方式直接访问 matplotlib 图

fig = myGridPlotObject.fig

就像之前在此线程中的其他帖子中建议的那样。

此答案适用于旧版本的 Seaborn,请参阅下面的较新答案

以下已过时:删除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")

上述一些解决方案对我不起作用。 尝试时未找到.fig属性,并且无法直接使用.savefig() 然而,有效的是:

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

我是一个新的 Python 用户,所以我不知道这是否是由于更新。 我想提一下,以防其他人遇到和我一样的问题。

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')

更新注意: size已更改为height

您应该只能够使用savefig的方法sns_plot直接。

sns_plot.savefig("output.png")

为清楚起见,如果您确实想访问sns_plot所在的 matplotlib 图,则可以直接使用

fig = sns_plot.fig

在这种情况下,您的代码假定没有get_figure方法。

我使用distplotget_figure成功保存图片。

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

这对我有用

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')

也可以只创建一个 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

在 seaborn 0.8.1 中使用sns.figure.savefig("output.png")会出错。

而是使用:

import seaborn as sns

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

我无法得到其他答案的工作,并最终得到它为我工作 matplotlib==3.2.1 。 如果您在 for 循环或某种迭代方法中执行此操作,则尤其如此。

sns.scatterplot(
    data=df_hourly, x="date_week", y="value",hue='variable'
)
plt.savefig('./f1.png')
plt.show()

请注意, savefig 必须在 show 调用之前。 否则将保存一个空图像。

仅供参考,以下命令在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM