如何在循环中保存多个绘图?

时间:2016-01-25 10:23:41

标签: python pandas plot seaborn

我试图在一个循环中保存多个绘图,但它将它们相互绘制。我该怎么办?

示例代码:

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)

1 个答案:

答案 0 :(得分:0)

您需要清除上一个图中的数据,该数据在循环中滚动。这应该有效,注意fig.clf()作为每个循环的结束:

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