【发布时间】:2020-06-12 11:18:20
【问题描述】:
我仍然对 Cartopy 的某些方面感到困惑。我不明白为什么有些指令不起作用...例如,如果我想画一张英国的地图,我可以使用下面的程序,然后我就得到了我的地图。
我知道Orthographic 投影使用度数表示经度和纬度,所以我设置extent = [-6.5, 3.5, 49.5, 59.0] 来限制我的地图,其中-6.5 表示东经的6.5°,3.5 表示西经的3.5°经度等
为此,如果我想在地图上突出显示格林威治,我认为我必须首先定义其坐标:Greenwich_lon, Greenwich_lat = 0.0, 51.0,然后绘制这些坐标:ax.plot(Greenwich_lon, Greenwich_lat, marker='x', markersize=12, color='red')。
但这在地图上给了我一个完全在正确位置之外的点!
为什么会这样?它来自不合适的ax.plot 吗?是不是来自ax.plot 中参数Greenwich_lon 和Greenwich_lat 的定位(我颠倒了它们,但没有任何改变)?它是否来自ax.plot 中缺少的某些指令?是否来自所使用的投影系统?
我在哪里可以找到简单的解释和例子?我知道 Cartopy 有一个庞大的文档 here,但它是如此之大,以至于我比其他任何东西都迷失了...
import matplotlib.pyplot as plt
import cartopy
import cartopy.feature as cf
import cartopy.crs as ccrs
import numpy as np
plt.figure(figsize=(8, 8))
extent = [-6.5, 3.5, 49.5, 59.0]
central_lon = np.mean(extent[:2])
central_lat = np.mean(extent[2:])
proj = ccrs.Orthographic(central_lon, central_lat)
ax = plt.axes(projection=proj)
ax.set_extent(extent)
ax.gridlines()
border = cf.NaturalEarthFeature(
'cultural', 'admin_0_boundary_lines_land', scale='110m')
land = cf.NaturalEarthFeature(
'physical', 'land', scale='110m',
edgecolor='black', facecolor=cfeature.COLORS['land'])
ocean = cf.NaturalEarthFeature(
'physical', 'ocean', scale='110m',
edgecolor='none', facecolor=cfeature.COLORS['water'])
ax.add_feature(border)
ax.add_feature(land)
ax.add_feature(ocean)
Greenwich_lon, Greenwich_lat = 0.0, 51.0
ax.plot(Greenwich_lon, Greenwich_lat, marker='x', markersize=12, color='red')
plt.show()
【问题讨论】:
-
将此选项
transform=ccrs.PlateCarree()添加到ax.plot()。它将纬度/经度转换为投影坐标,并在预期位置绘图。 -
在
.set_extent()中,默认的 ccrs 是以度为单位的纬度/经度,因此您可以跳过其中的crs=ccrs.PlateCarree()选项。
标签: python-3.x matplotlib cartopy