循环保存netcdf图像pycdf和numpy更改时间片

时间:2015-11-03 11:09:41

标签: python numpy plot netcdf

我有一个netcdf文件,显示变量在不同时间片的变化情况。 我想保存此时间片的.png,然后保存下一个时间片的单独.png,直到保存所有时间片。我想将每个时间片保存为数字。

我的尝试如下,但我无法获得保存不同文件的循环。

    nc=pycdf.CDF("netcdf1.nc",pcdf.NC.NOWRITE)
    nc.automode()
    nc2=pycdf.CDF("netcdf2.nc",pcdf.NC.NOWRITE)
    nc.automode()
    for i in range(5):
        time = i
        lons  = nc2.var('lon')[0,1:382,1:440]
        lats  = nc2.var('lat')[0,1:382,1:440]
        thk=nc.var('thk')[time,1:382,1:440]
        m = Basemap(width=2200000,height=1910000,resolution='l',projection='aea',lat_0=lats.mean(),lon_0=lons.mean())
        x,y = m(*(lons,lats))
        cs=m.pcolor(x,y,thk,norm=colors.LogNorm())
        plt.savefig('/home/jeremy/Desktop/Figures_presentation/Hagdorn_SIA/Vel/value{i}.png')
        nc.close()

1 个答案:

答案 0 :(得分:0)

您必须将plt.savefig命令更改为:plt.savefig('/home/jeremy/Desktop/Figures_presentation/Hagdorn_SIA/Vel/value'+i+'.png')

或者如果你想要001,002,003之类的数字     plt.savefig('/home/jeremy/Desktop/Figures_presentation/Hagdorn_SIA/Vel/value{0:03d}.png'.format(i))

另外,从循环中取出basemap命令会更快一些。

相关问题