cartopy - 对空 Cartopy 流图进行故障排除
问题描述
我正在尝试使用 Cartopy (0.17.0) 绘制矢量数据的流线,但结果图是空的。使用直接 matplotlib (3.1.1) 有效。箭头 PatchCollection 和行 LineCollection 的数组和路径属性为空。
我尝试了不同的投影,明确地创建x
, y
,v
和u
from 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
)
解决方案
为了回答我自己的问题,我找到了以下解决方法:
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()
)
推荐阅读
- php - 使用带有副本集的 PHP mongodb 驱动程序从 MongoDB 读取时速度慢
- java - 有没有办法在 Oracle/JDBC 中区分 DATE 和 TIMESTAMP?
- sql - 如何根据当前月份和年份从表中获取上个月的数据?
- javascript - JavaScript - 遍历所有 HTML 元素并选择最后一个
- c# - 如何检查一个字符串在另一个字符串中存在多少次
- android - Android:矢量资源不出现
- javascript - 如何仅在静态 URL 中插入元标记
- java - 我将如何在 c# 中实现 java 代码,反之亦然?
- jquery - 将文档 keyup 事件过滤到没有选择器的特定元素
- django - 计算另一个模型中的对象数量