numpy 来自NetCDF数据的4D曲面图

zujrkrfu  于 2023-06-23  发布在  Etcd
关注(0)|答案(1)|浏览(253)

我正在尝试绘制netcdf数据(attached file)的4D曲面图。它有四个维度:lat、long、lev和值-灰尘混合比(5个值DU 01、DU 02…05)。因为我对Python非常陌生,所以我对它了解不多。我必须绘制DU 01(这将是填充值)与纬度,长度和水平。示例图我需要的是x1c 0d1x。
任何帮助将是非常有益的和赞赏。
我得到这个错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[59], line 9
      7 fig = plt.figure(figsize=(12, 8))
      8 ax = fig.add_subplot(111, projection='3d')
----> 9 ax.plot_surface(lon_3d, lat_3d, lev_3d, facecolors=plt.cm.jet(DMR/np.nanmax(DMR)), rstride=1, cstride=1, edgecolor='none', alpha=.8)
     10 ax.set_title('DU01 dust mixing ratio')
     11 ax.set_xlabel('Longitude')

File C:\ProgramData\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:1381, in Axes3D.plot_surface(self, X, Y, Z, norm, vmin, vmax, lightsource, **kwargs)
   1378 had_data = self.has_data()
   1380 if Z.ndim != 2:
-> 1381     raise ValueError("Argument Z must be 2-dimensional.")
   1383 Z = cbook._to_unmasked_float_array(Z)
   1384 X, Y, Z = np.broadcast_arrays(X, Y, Z)

ValueError: Argument Z must be 2-dimensional.
wsxa1bj1

wsxa1bj11#

此代码创建DU01混合比值数据的3D曲面图。

import xarray as xr
import plotly.graph_objs as go

# Load the netcdf file into an xarray dataset
ds = xr.open_dataset('your_file.nc')

# Select the DU01 variable and the lat, long, and lev dimensions
du01 = ds['value-dust_mixing_ratio'].sel(value='DU01')
lat = ds['lat'][:]
lon = ds['lon'][:]
lev = ds['lev'][:]

# Create a 3D meshgrid of the lat, long, and lev coordinates
lon_3d, lat_3d, lev_3d = np.meshgrid(lon, lat, lev, indexing='ij')

# Create the plotly figure object
fig = go.Figure(data=[go.Surface(z=lev_3d, x=lon_3d, y=lat_3d, surfacecolor=du01, colorscale='Jet')])

# Customize the layout
fig.update_layout(title='DU01 Dust Mixing Ratio',
                  scene=dict(xaxis_title='Longitude', yaxis_title='Latitude', zaxis_title='Level'))

# Show the plot
fig.show()

相关问题