You need to enable JavaScript to run this app. Cartopy PlateCarree变换下Matplotlib单箭头quiver绘图报错问题
优惠活动
大模型
产品
解决方案
定价
更多

Cartopy PlateCarree变换下Matplotlib单箭头quiver绘图报错问题

Cartopy地图单风矢量绘制触发ndim属性报错问题

问题描述

尝试在带地理投影的地图上通过plt.quiver在单个坐标点绘制风矢量,指定ccrs.PlateCarree()作为坐标变换时,底层子函数触发“尝试获取0维整数维度数”的报错。当前运行环境版本为cartopy 0.18.0、matplotlib 3.2.1,不存在遗漏相关配置的问题。

最小可复现代码

from matplotlib import pyplot as plt
import cartopy 
import cartopy.crs as ccrs
%matplotlib inline

#### 可正常运行:无地图轴的基础quiver绘图
plt.quiver(0,0,1,-1)


#### 可正常运行:带地图轴但不指定transform的quiver绘图(坐标位置不符合地理投影规则,仅用于对比运行/异常边界)
plt.subplot(projection=ccrs.EckertIV())
plt.quiver(0,0,1,-1)

#### 可正常运行:地图轴投影与transform一致的quiver绘图
plt.subplot(projection=ccrs.EckertIV())
plt.quiver(0,0,1,-1,transform=ccrs.EckertIV())

#### 运行报错:地图轴投影与transform不一致、使用PlateCarree变换的标量输入quiver绘图
plt.subplot(projection=ccrs.EckertIV())
plt.quiver(0,0,1,-1,transform=ccrs.PlateCarree())

触发的报错信息

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-93-dec36b09994d> in <module>
      1 plt.subplot(projection=ccrs.EckertIV())
----> 2 plt.quiver(0,0,1,-1,transform=ccrs.PlateCarree())

~/.conda/envs/climate1/lib/python3.7/site-packages/matplotlib/pyplot.py in quiver(data, *args, **kw)
   2791 def quiver(*args, data=None, **kw):
   2792     __ret = gca().quiver(
-> 2793         *args, **({"data": data} if data is not None else {}), **kw)
   2794     sci(__ret)
   2795     return __ret

~/.conda/envs/climate1/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py in wrapper(self, *args, **kwargs)
    308 
    309         kwargs['transform'] = transform
--> 310         return func(self, *args, **kwargs)
    311     return wrapper
    312 

~/.conda/envs/climate1/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py in quiver(self, x, y, u, v, *args, **kwargs)
   1837             # Transform the vectors if the projection is not the same as the
   1838             # data transform.
-> 1839             if (x.ndim == 1 and y.ndim == 1) and (x.shape != u.shape):
   1840                 x, y = np.meshgrid(x, y)
   1841             u, v = self.projection.transform_vectors(t, x, y, u, v)

AttributeError: 'int' object has no attribute 'ndim'

现有临时规避方案

目前常用的临时方法是传入重复的双值数组,强制触发NumPy数组类型检查,绕过原生标量的属性兼容问题:

import numpy as np
plt.subplot(projection=ccrs.EckertIV())
plt.quiver(np.array([0,0]),np.array([0,0]),np.array([1,1]),np.array([-1,-1]),transform=ccrs.PlateCarree())

根因与标准实现方式

  • 该问题属于Cartopy 0.18.0版本GeoAxes.quiver的实现缺陷:当传入的x/y/u/v为Python原生整数/浮点数这类0维标量、且指定的坐标变换与轴本身投影不一致时,代码直接尝试访问标量的ndim属性,但Python原生标量不存在该属性,最终触发报错
  • 当变换和轴投影一致时,代码不会进入向量投影转换的分支,因此不会触发该段检查逻辑,也就不会报错
  • 不需要重复绘制两次相同箭头造成冗余,只需要把传入的标量转为单元素的1维NumPy数组即可,运行效果与重复双值的方案完全一致:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

plt.subplot(projection=ccrs.EckertIV())
plt.quiver(np.array([0]), np.array([0]), np.array([1]), np.array([-1]), transform=ccrs.PlateCarree())

内容的提问来源于stack exchange,提问作者ks905383

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 04:24:03