如何用 Cartopy 绘制洋流

How to plot Ocean Currents with Cartopy

我正在尝试为一个项目绘制包含来自 NASA 数据库的洋流的 netCDF4 文件,但我不断收到诸如 "x and y coordinates are not compatible with the shape of the vector components".

之类的错误

我曾尝试将 streamplot 更改为 contourf(当我这样做时说它需要是一个二维数组)我尝试更改但我无法让它工作。

import os
import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np
import cartopy.crs as ccrs

fname = "oscar_vel2019.nc.gz.nc"

data=netcdf_dataset(fname)
v = data.variables['v'][0, :, :, :]
vf = data.variables['vm'][0, :, :, :]
u = data.variables['u'][0, :, :, :]
uf = data.variables['um'][0, :, :, :]
lats = data.variables['latitude'][:]
lons = data.variables['longitude'][:]
ax = plt.axes(projection=ccrs.PlateCarree())

mymap=plt.streamplot(lons, lats, u, v, 60, transform=ccrs.PlateCarree())

ax.coastlines()

plt.show()

我希望它能在绘图中显示洋流,并通过动画显示洋流中粒子的运动。我真的对此了解不多,这就是我问的原因。这是我从中获取文件的 link:https://podaac-opendap.jpl.nasa.gov/opendap/hyrax/allData/oscar/preview/L4/oscar_third_deg/oscar_vel2019.nc.gz.html

好的,我下载了数据。问题是u和v是4维的,所以需要挤掉"depth"维。 Cartopy 也不接受大于 180 的经度,并且您可能无法通过流式绘制整个事情。此外,密度=60 将永远...

这很丑陋,但给了你想法。

import xarray as xr
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

with xr.open_dataset('/Users/jklymak/downloads/oscar_vel2019.nc.gz.nc') as ds:
    print(ds)

    ax = plt.axes(projection=ccrs.PlateCarree())

    dec = 10
    lon = ds.longitude.values[::dec]
    lon[lon>180] = lon[lon>180] - 360
    mymap=plt.streamplot(lon, ds.latitude.values[::dec], ds.u.values[0, 0, ::dec, ::dec], ds.v.values[0, 0, ::dec, ::dec], 6, transform=ccrs.PlateCarree())
    ax.coastlines()
    plt.show()