次のコードを試しました(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
存在し、次のようになると予想しています。
どうすれば問題を解決できますか?
ベストアンサー1
次の呼び出しにより、図にアクセスできます (Seaborn 0.8.1 互換)。
swarm_plot = sns.swarmplot(...)
fig = swarm_plot.get_figure()
fig.savefig("out.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のオブジェクトを使用してPairGrid
、次のようなプロットを生成しました。この例この場合、 は、GridPlot
たとえば のようなプロット オブジェクトではないためsns.swarmplot
、関数はありませんget_figure()
。次の方法で、matplotlib の図に直接アクセスできます。
fig = myGridPlotObject.fig