正交投影错误地绘制了Cartopy绘图点

时间:2018-11-07 18:24:26

标签: python cartopy

我正在尝试使用带有Anaconda Python的Cartopy绘制地图点,但由于转换而遇到一些奇怪的失败。在我的简单示例中,我试图绘制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.3proj4==5.0.1proj4==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工作吗?

1 个答案:

答案 0 :(得分:2)

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

首先,明确变换要位于本机投影 ...

中的点
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