How to save a Seaborn plot into a file

后端 未结 10 1059
庸人自扰
庸人自扰 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 09:04

    The suggested solutions are incompatible with Seaborn 0.8.1

    giving the following errors because the Seaborn interface has changed:

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

    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

    fig = myGridPlotObject.fig
    

    Like previously suggested in other posts in this thread.

提交回复
热议问题