gpt4 book ai didi

python - Cartopy 绘图点与正交投影不正确

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:52 32 4
gpt4 key购买 nike

我正在尝试使用 Cartopy 和 Anaconda Python 绘制 map 点,但在转换时遇到了一些奇怪的失败。在我的简单示例中,我试图绘制 3 个点,但它们正在加倍。

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

lons = [214.5, 2.7, 197.5]
lats = [35, 36, 37.]

ax = plt.axes(projection=ccrs.Orthographic(
central_longitude=0,
central_latitude=90))
# plot lat/lon points
ax.plot(lons, lats, 'ro',
transform=ccrs.Geodetic())
# plot north pole for reference
ax.plot([0], [90], 'b^',
transform=ccrs.Geodetic())
# add coastlines for reference
ax.coastlines(resolution='50m')
ax.set_global()

plt.show()

Plot output

测试:

cartopy==0.16.0Cartopy-0.16.1.dev179-

proj4==4.9.3, proj4==5.0.1, proj4==5.0.2

我唯一的提示是,使用 Cartopy-0.16.1.dev179-proj4==5.0.1,我得到了这个 UserWarning :

/Users/***/anaconda3/lib/python3.6/site-packages/cartopy/crs.py:1476: UserWarning: The Orthographic projection in Proj between 5.0.0 and 5.1.0 incorrectly transforms points. Use this projection with caution.

我在 https://github.com/SciTools/cartopy/issues/1172 上开了一个问题但问题已关闭。任何人都知道如何让 cartopy 与正交投影一起正常工作?

最佳答案

据我所知,您可以使用多种方法来获得预期的结果。

首先,显式转换要在原生投影中的点...

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

# create the lat/lon points
lons = np.array([214.5, 2.7, 197.5])
lats = np.array([35, 36, 37.])

# create the projections
ortho = ccrs.Orthographic(central_longitude=0, central_latitude=90)
geo = ccrs.Geodetic()

# create the geoaxes for an orthographic projection
ax = plt.axes(projection=ortho)

# transform lat/lons points to othographic points
points = ortho.transform_points(geo, lons, lats)

# plot native orthographic points
ax.plot(points[:, 0], points[:, 1], 'ro')

# plot north pole for reference (with a projection transform)
ax.plot([0], [90], 'b^', transform=geo)

# add coastlines for reference
ax.coastlines(resolution='50m')
ax.set_global()

这个情节符合预期......

Expected Orthographic projection plot

您看到的最初问题是 cartopy 试图将点序列解释为有界几何(或路径),但有点困惑。将纬度/经度点显式转换为原生正交点可避免此子弹。

了解这些信息后,我们可以选择调用适当的方法,将点列表视为单个点(并避免 cartopy 做出不符合我们期望的假设),方法是使用 scatter 而不是 plot...

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

# create the lat/lon points
lons = np.array([214.5, 2.7, 197.5])
lats = np.array([35, 36, 37.])

# create the projections
ortho = ccrs.Orthographic(central_longitude=0, central_latitude=90)
geo = ccrs.Geodetic()

# create the geoaxes for an orthographic projection
ax = plt.axes(projection=ortho)

# plot native orthographic scatter points
ax.scatter(lons, lats, marker='o', c='r', transform=geo)

# plot north pole for reference
ax.plot([0], [90], 'b^', transform=geo)

# add coastlines for reference
ax.coastlines(resolution='50m')
ax.set_global()

这也适用于我。

HTH

关于python - Cartopy 绘图点与正交投影不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195551/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com