【问题标题】:Borders and coastlines interfering in Python Cartopy边界和海岸线干扰 Python Cartopy
【发布时间】:2020-06-10 16:48:01
【问题描述】:

我正在尝试使用 Cartopy 绘制法国地图,但我对它不太满意。

在网上到处搜索Python代码,我构建了以下程序:

import cartopy
import cartopy.feature as cfeature
import cartopy.crs as ccrs

import numpy as np

extent = [-4.25, 7.5, 42.25, 51]
central_lon = np.mean(extent[:2])
central_lat = np.mean(extent[2:])

plt.figure(figsize=(8, 8))

ax = plt.axes(projection=ccrs.AlbersEqualArea(central_lon, central_lat))

ax.set_extent(extent)
ax.gridlines()

rivers_50m = cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', '50m')

ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=1)
ax.add_feature(cartopy.feature.OCEAN,facecolor=("lightblue"))
ax.add_feature(cartopy.feature.LAND, edgecolor='black')
ax.add_feature(cartopy.feature.LAKES, edgecolor='black')
ax.add_feature(rivers_50m, facecolor='None', edgecolor='blue', linestyle=':')
ax.coastlines(resolution='10m', color='red', linestyle='-', alpha=1)

plt.show()

这导致以下地图:

这几乎是我想要的,但是......我不明白为什么原始边界(深色虚线)会干扰海滨的海岸线(波浪形红线),这真的很难看。

有什么方法可以解决这个问题? (知道我想保持陆地边界不变)。

【问题讨论】:

  • 您混合了数据的分辨率。 10m 的海岸线和110m 的陆地(默认分辨率)不匹配。
  • @swatchai,我理解你的意思,但你会如何在 Python 程序中更准确地改变它?
  • 这个答案很有用。一票赞成。

标签: python-3.x matplotlib cartopy


【解决方案1】:

这是可运行的代码。现在绘制的所有特征都具有相同的scale 或分辨率。可用的标尺有:110m、50m、10m。

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

extent = [-4.25, 7.5, 42.25, 51]
central_lon = np.mean(extent[:2])
central_lat = np.mean(extent[2:])

plt.figure(figsize=(8, 8))

ax = plt.axes(projection=ccrs.AlbersEqualArea(central_lon, central_lat))
ax.set_extent(extent)
ax.gridlines()

resol = '50m'  # use data at this scale
bodr = cartopy.feature.NaturalEarthFeature(category='cultural', 
    name='admin_0_boundary_lines_land', scale=resol, facecolor='none', alpha=0.7)
land = cartopy.feature.NaturalEarthFeature('physical', 'land', \
    scale=resol, edgecolor='k', facecolor=cfeature.COLORS['land'])
ocean = cartopy.feature.NaturalEarthFeature('physical', 'ocean', \
    scale=resol, edgecolor='none', facecolor=cfeature.COLORS['water'])
lakes = cartopy.feature.NaturalEarthFeature('physical', 'lakes', \
    scale=resol, edgecolor='b', facecolor=cfeature.COLORS['water'])
rivers = cartopy.feature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', \
    scale=resol, edgecolor='b', facecolor='none')

ax.add_feature(land, facecolor='beige')
ax.add_feature(ocean, linewidth=0.2 )
ax.add_feature(lakes)
ax.add_feature(rivers, linewidth=0.5)
ax.add_feature(bodr, linestyle='--', edgecolor='k', alpha=1)

plt.show()

还有剧情:

【讨论】:

  • 这令人印象深刻。我看到我在互联网研究中错过了很多......它让你想继续深入Cartopy。谢谢。
猜你喜欢
  • 2017-12-19
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2017-08-02
  • 2020-07-24
相关资源
最近更新 更多