MatplotlibCartopy Streamplot会生成一些投影的QhullError

2024-04-19 08:47:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个正交投影上绘制全局数据的流函数,但这似乎在向量变换中出现了中断。也许我遗漏了处理这个问题的transform关键字?我尝试了各种各样的投影:一些有效,许多没有。是否可以使用streamplot对全局数据进行正交(或类似)投影?在

我使用的是python3.6、numpy 1.14.3、xarray 0.10.3、matplotlib 2.2.2和cartopy0.16.0。在

下面是一个例子:

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fakelon = np.linspace(-180, 180, 288)
fakelat = np.linspace(-90, 90, 192)
u = xr.DataArray(np.random.rand(len(fakelat), len(fakelon)), coords=[fakelat, fakelon], dims=['lat', 'lon'])
v = xr.DataArray(np.random.rand(len(fakelat), len(fakelon)), coords=[fakelat, fakelon], dims=['lat', 'lon'])
x,y = np.meshgrid(u['lon'], u['lat'])
fig, ax = plt.subplots(subplot_kw={'projection':ccrs.Orthographic()})
ax.set_global()
ax.coastlines()
ax.streamplot(x, y, u.values, v.values, transform=ccrs.PlateCarree())
plt.show()

这导致

^{pr2}$

Tags: 数据importlenasnptransformpltax
2条回答

问:有没有可能在全局数据上使用流图和正交(或类似)投影?在

A:在正字法上是的。前提是生成绘图的点阵列不超出投影的可见区域。在

下面是要尝试的工作代码和一个示例图。在

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

# prepare points array that are located on the ...
#  visible part of the orthographic projection
slon = np.linspace(-90., 90., 8)   # no further than +- 90 deg
slat = np.linspace(-45., 45., 6)   # take points within +-45 deg
sx,sy = np.meshgrid(slon, slat)    # create meshgrid

# prep vector data (us,vs) for stream plot
us, vs = np.ones(sx.shape), np.ones(sy.shape)
us = us * (0.5+np.random.normal(0,1))*10
vs = vs * (0.5+np.random.normal(0,1))*10

fig, ax = plt.subplots(subplot_kw={'projection': ccrs.Orthographic()})
fig.set_size_inches([8,8])

ax.set_global()
ax.stock_img()
ax.coastlines(linewidth=0.5)

# do the streamplot
ax.streamplot(sx, sy, us, vs, transform=ccrs.PlateCarree())

plt.show()  # result will be different for each run

结果图:

enter image description here

我不认为我能看到你的目标范围是什么,但正射投影有时会有问题,因为它不是全球代表性的。我的意思是,你的数据看起来是全局的,你试图把所有的数据转换成一个投影,它在任何给定的时间只能代表地球的一部分,而它目前所代表的特定部分可以改变数据的转换计算。在

也许您可以尝试将目标范围限制在投影范围内。在

相关问题 更多 >