Stop seaborn plotting multiple figures on top of one another

前端 未结 3 712
太阳男子
太阳男子 2020-12-23 20:21

I\'m starting to learn a bit of python (been using R) for data analysis. I\'m trying to create two plots using seaborn, but it keeps saving the second on top of

3条回答
  •  别那么骄傲
    2020-12-23 20:58

    I agree with a previous comment that importing matplotlib.pyplot is not the best software engineering practice as it exposes the underlying library. As I was creating and saving plots in a loop, then I needed to clear the figure and found out that this can now be easily done by importing seaborn only:

    import seaborn as sns
    
    data = np.random.normal(size=100)
    path = "/path/to/img/plot.png"
    
    plot = sns.distplot(data)
    plot.get_figure().savefig(path)
    plot.get_figure().clf() # this clears the figure
    
    # ... continue with next figure
    

提交回复
热议问题