簡體   English   中英

如何使用 netcdf 數據在 matlab 中制作輪廓 plot

[英]How to make contour plot in matlab using netcdf data

我使用 NetCDF 數據在 python 中創建了輪廓 plot 。 我正在繪制的是 x 方向上的速度,形狀為 (1, 124, 128, 128),其中尺寸為 (time, z, y, x)。 我迷路的地方是讓我的 plot 在 MATLAB 中工作。 在 python 中工作的代碼是,

import netCDF4 as NC
from netCDF4 import Dataset as NetCDFFile 
import matplotlib.pyplot as plt
import numpy as np

nc = NetCDFFile('XYZ_time_1.nc')
u = nc.variables["u"][:,:,:,:] # Velcity in x direction, size(1, 124, 128, 128), [t,z,y,x]
plt.contourf(u[0,:,:,0]) # time[0], z[:], y[:], x[0]

我在 MATLAB 中的翻譯嘗試如下。 數組的大小與 python 以大小 (128, 128, 124, 1) 和維度 (x, y, z, time) 解釋的大小相反。 我是 MATLAB 的新手,不確定如何復制代碼。 在 MATLAB 中,時間維度不包含在 size() function 中。 如何使用數組切片在 MATLAB 中制作輪廓 plot 以復制上述 python 代碼中的結果?

filename = 'XYZ_time_1.nc';
ncdisp(filename);
u = ncread(filename, 'u'); # size(128, 128, 124), time not included?
contour(:,:,1) # (x, y, z)

上面的 MATLAB 代碼將適用於 python 中的特殊情況。 數組索引是我開始迷路的地方。 我希望能夠 y[:] vs z[:] 獲得固定的 x[index]。 你如何安排索引切片來完成這項工作?

print(np.shape(nc.variables["u"][0,0,:,:])) # t[0], z[0], y[:],x[:]
plt.contourf(nc.variables['u'][0,0,:,:])

我已經能夠使用 MATLAB 置換 function 來回答我自己的問題。 如果您嘗試將 python 中的數組切片轉換為 MATLAB,則如下所示。

# In python 
import netCDF4 as NC
from netCDF4 import Dataset as NetCDFFile 
import matplotlib.pyplot as plt
import numpy as np

nc = NetCDFFile('XYZ_time_1.nc')
u = nc.variables["u"][:,:,:,:]
plt.contourf(u[0,:,:,0]) # time[0], z[:], y[:], x[0]

轉換為 MATLAB 是

% In MATLAB
S = permute(u, [3,2,1]) % rearranges the vector to be [z, y, x] 
contourf(S(:,:,1))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM