How to save a Seaborn plot into a file

后端 未结 10 1046
庸人自扰
庸人自扰 2020-12-07 08:43

I tried the following code (test_seaborn.py):

import matplotlib
matplotlib.use(\'Agg\')
import matplotlib.pyplot as plt
matplotlib.style.use(\'g         


        
相关标签:
10条回答
  • 2020-12-07 08:53

    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.

    0 讨论(0)
  • 2020-12-07 08:56

    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')
    
    0 讨论(0)
  • 2020-12-07 08:56

    Just FYI, the below command worked in seaborn 0.8.1 so I guess the initial answer is still valid.

    sns_plot = sns.pairplot(data, hue='species', size=3)
    sns_plot.savefig("output.png")
    
    0 讨论(0)
  • 2020-12-07 08:59

    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")
    
    0 讨论(0)
  • 2020-12-07 09:03

    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')
    
    0 讨论(0)
  • 2020-12-07 09:04

    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")
    
    0 讨论(0)
提交回复
热议问题