How to save multiple plot in a loop?

前端 未结 1 1324
既然无缘
既然无缘 2021-01-24 23:46

I tried to save multiple plot in a loop, but It draw them over each other. what should I do?

sample code:

import pandas as pd
import seaborn as sns
data=         


        
相关标签:
1条回答
  • 2021-01-25 00:03

    You need to clear the data from the previous figure which is rolling over in the loop. This should work, noting fig.clf() as the end of each loop:

    import pandas as pd
    import seaborn as sns
    data=pd.DataFrame({'a':[1,2,3,4,5,6],'b':[0,1,1,0,1,1],'c':[0,0,0,1,1,1]})
    for i in ['b','c']:
        img=sns.boxplot(data.a, groupby=data[i])
        fig = img.get_figure()
        fig.savefig(i)
        fig.clf()
    
    0 讨论(0)
提交回复
热议问题