问题描述
我正在对一段代码进行最小扩展。
我有一个 make_fig
函数,它曾经只生成一个图形,我会在许多其他函数中将其称为 fig
,然后将其保存为 fig.savefig
。
在扩展中,make_fig
现在返回一个元组数字。所以,为了保存它们,我现在需要这样的东西:
fig = make_fig
for f in fig:
f.savefig
我希望有一种更优雅的解决方案,不需要在出现 make_fig
的任何地方添加 for 循环。
如果 a.savefig
是一个 a
实例,我可以以某种方式修改 matplotlib.pytplot
方法,使其执行其正常功能,并且如果它是一个元组,则执行上面的 for 循环吗?
下面的 MWE。
d = 1
是“旧”代码,d=2
是我要添加的扩展。
import matplotlib.pyplot as plt
def make_fig(d):
if d==1:
fig,ax = plt.subplots(1)
elif d==2:
fig1,ax = plt.subplots(1)
fig2,ax = plt.subplots(1)
fig = (fig1,fig2)
else:
raise Exception('error')
return fig
d=2
fig = make_fig(d)
fig.savefig('hello.png')
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)