【问题标题】:Highlight places in Python Cartopy maps在 Python Cartopy 地图中突出显示地点
【发布时间】: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_lonGreenwich_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


【解决方案1】:

Cartopy 始终在 projection 坐标中绘图(与 Matplotlib 中的 data 坐标相同)。一般来说,它接受大多数投影坐标中的输入数据,并带有指定数据所基于的坐标系的选项。

您有责任使用正确的值和随附的选项。 例如,如果 XXXX() 是您的数据所基于的地图投影坐标,

.set_extent(lon_min, lon_max, lat_min, lat_max, crs=cartopy.crs.XXXX())

.plot(lon, lat, marker'x', transform=cartopy.crs.XXXX())

如果您使用 lat/long 以度为单位,您可以使用 PlateCarree() 代替上面的 XXXX()。 但是,为了方便起见, .set_extent() 将 crs=cartopy.crs.PlateCarree() 作为默认选项,您通常会忽略。

有关坐标系和转换的更多信息:

卡托比 https://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html 由于 Cartopy 是建立在 Matplotlib 之上的,因此您需要查看 Matplotlib 中的相关主题。

Matplotlib https://matplotlib.org/3.2.1/tutorials/advanced/transforms_tutorial.html

【讨论】:

  • 好的@swatchai,它让我更好地了解发生了什么。感谢您提供的具体链接,我会仔细阅读。
猜你喜欢
  • 2019-09-25
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2020-02-07
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多