首页 > 解决方案 > 对空 Cartopy 流图进行故障排除

问题描述

我正在尝试使用 Cartopy (0.17.0) 绘制矢量数据的流线,但结果图是空的。使用直接 matplotlib (3.1.1) 有效。箭头 PatchCollection 和行 LineCollection 的数组和路径属性为空。

我尝试了不同的投影,明确地创建x, y,vufrom cartopy.vector_transform.vector_scalar_to_grid(),删除/更改范围,从速度场中删除 nans。

简单的可重现示例:

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

x = np.array([233., 236.83146159, 240.66292318, 244.49438477])
y = np.array([28., 31.645003, 35.290006, 38.935009])
u = np.array(
      [[ 0.03955199, -0.22247993, -0.25873835,  0.04202399],
       [-0.23483805,  0.09814767, -0.10702853,  np.nan],
       [ 0.21835904,  0.06390616, np.nan,       np.nan],
       [ 0.19803461,  np.nan,     np.nan,       np.nan]]
)
v = np.array(
    [[-0.0986053 ,  0.05887021, -0.25543633, -0.20215461],
     [ 0.11536164, -0.11004942, -0.01419378, np.nan],
     [-0.06482275, -0.35679315, np.nan,      np.nan],
     [ 0.16507462, np.nan,      np.nan,      np.nan]]
)
ax = plt.subplot(projection=ccrs.PlateCarree())
ax.streamplot(
    x, 
    y,
    u,
    v,
)

这有效:

ax = plt.subplot()
plt.streamplot(
    x,
    y,
    u,
    v
)

标签: cartopy

解决方案


为了回答我自己的问题,我找到了以下解决方法:

    from cartopy.vector_transform import vector_scalar_to_grid
    from matplotlib.axes import Axes

    ax = plt.subplot(projection=ccrs.PlateCarree())
    new_x, new_y, new_u, new_v, = vector_scalar_to_grid(
        ccrs.PlateCarree(),
        ccrs.PlateCarree(),
        5,
        x,
        y,
        u,
        v
    )

    Axes.streamplot(
        ax,
        new_x,
        new_y,
        new_u,
        new_v,
        transform=ccrs.PlateCarree()
    )

推荐阅读