最近在绘制等值线图,由于绘制图片的时间序列以及数量是非常大的,所以必须要用到自动创建路径自动保存,所以把一些经验放到这里啦
循环体中保存图的内容如下:
path = 'F:/contour-outcome'+'/'+str(picnum)+'/'
if not os.path.exists(path): # 如果不存在路径,则创建这个路径
os.makedirs(path)
print(path)
plt.savefig(path+'/'+str(timenum)+'.jpg')
plt.close()
主要在于不存在的路径的创建,以及将图片顺序存放于对应文件夹,效果如下:
F:\i-File\2021\igsg0010.21i
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:/contour-outcome/1/
F:\i-File\2021\igsg0020.21i
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
F:/contour-outcome/2/
下面是文件1
下面是文件2
由于每天的图片都为13个,因此上述代码中的timenum需要在循环中不断自增,并且在到下一天时恢复到初值,这里不方便展示,请小伙自行理解哈,不懂得地方或者指正的地方欢迎留言啦!