【发布时间】:2017-09-25 19:49:29
【问题描述】:
我需要将函数的输出保存到一个文件中,其中包含传递给该函数的变量名称。 我尝试使用来自 stackoverflow 用户的函数。
def var_name(**variables):
return [x for x in variables]
我的代码如下,dist 是包含不同数组的 numpy 数组数据集。
MTnum=10
import matplotlib.pyplot as plt
import numpy as np
def Plot(dist, sav=False):
MT_dat = np.vsplit(dist,MTnum)
for i,mtdat in enumerate(MT_dat):
plt.figure(i)
for j in range(len(mtdat[0])-1):
plt.plot(MT_dat[i][:,0],MT_dat[i][:,j+1])
plt.xlabel('time')
plt.ylabel('distance')
plt.title('MT_'+str(i+1)+var_name(dist=dist)+'.png')
if sav == True:
plt.savefig('MT_'+str(i+1)+var_name(dist=dist)+'.png')
plt.show()
如果我使用函数Plot(set1),文件将使用“dist”后缀而不是“set1”保存。请提出正确的做法。
【问题讨论】:
标签: python variable-names