gpt4 book ai didi

python - 循环脚本以生成多个图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:35 25 4
gpt4 key购买 nike

我有一个 Netcdf 数据集,其维度为 [时间、高度、纬度、经度],已使用 xarray 打开。我编写了一个代码,将特定时间戳的所有数据投影到 cartopy map 上,并将图像保存到我的目录中。我想为每个时间戳创建一个图像,但目前我知道如何执行此操作的唯一方法是手动更改时间戳条目并再次运行代码。由于有 360 个时间戳,这显然需要一些时间。我知道 Python for 循环很方便,但我对它们很不熟悉,所以有没有一种方法可以将此代码嵌入到循环中,以便我可以一次保存多个图像?

pv=data1.pv*10000
pv850=pv[:,0,:,:]
lons=pv850.longitude
lats=pv850.latitude

Fig = plt.figure(figsize=[10,8])
ax = plt.axes(projection=ccrs.NorthPolarStereo())
normi = mpl.Normalize(vmin=-1.5, vmax=12)
cs = ax.contourf(lons, lats, pv850[0,:,:], 50,
transform=ccrs.RotatedPole(), extend='both',
cmap='jet')
plt.colorbar(cs)
ax.coastlines()
ax.gridlines(crs=ccrs.Geodetic(), linestyle='--')

theta = np.linspace(0, 2*np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)

plt.title('Polar Plot 01-01-2018 00:00')
plt.savefig('image1.png')

ax.contourf(lons, lats, pv850[0,:,:] 行控制时间戳,其中“0”对应于时间戳条目(范围从 0-359)。I如果可能的话,还希望标题中的时间戳随每个情节而变化。最后,这是最终情节的图片。

enter image description here

最佳答案

将其放入循环中似乎很简单,所以我不确定为什么这如此困难。不过,您可以尝试以下方法。在这里,我将一些定义移到了 for 循环之外,因为您不需要一次又一次地定义它们 360 次。

theta = np.linspace(0, 2*np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)

for i in range(360):
Fig = plt.figure(figsize=[10,8])
ax = plt.axes(projection=ccrs.NorthPolarStereo())
normi = mpl.Normalize(vmin=-1.5, vmax=12)
cs = ax.contourf(lons, lats, pv850[i,:,:], 50,
transform=ccrs.RotatedPole(), extend='both',
cmap='jet')
plt.colorbar(cs)
ax.coastlines()
ax.gridlines(crs=ccrs.Geodetic(), linestyle='--')
ax.set_boundary(circle, transform=ax.transAxes)

plt.title('Polar Plot 01-01-2018 00:{:02d}'.format(i))
plt.savefig('image%s.png' %i)

关于python - 循环脚本以生成多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496539/

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