如何在循环中保存多个绘图? [英] How to save multiple plot in a loop?

查看:59
本文介绍了如何在循环中保存多个绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

示例代码:

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() 作为每个循环的结束:

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

这篇关于如何在循环中保存多个绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆