gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:51:44 25 4
gpt4 key购买 nike

我试图在一个循环中保存多个图,但它把它们画在了彼此之上。我该怎么办?

示例代码:

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

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

关于python - 如何在循环中保存多个图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34990179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com