【问题标题】:Geopandas - map and locaton plottingGeopandas - 地图和位置绘图
【发布时间】:2019-09-05 09:10:31
【问题描述】:

感谢question 的回答,我可以绘制地理熊猫世界地图,其中大陆和海洋以不同的投影方式着色。

现在我想补充几点,例如geopandas中包含的城市

cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))

不幸的是,这些城市被填满的大陆所覆盖。有没有办法让这些城市出现在地图的前面或顶部?

我当前的代码如下所示:

facecolor = 'sandybrown'
edgecolor = 'black'
ocean_color = '#A8C5DD'

crs1 = ccrs.NorthPolarStereo()

world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))

w1 = world.to_crs(crs1.proj4_init)
c1 = cities.to_crs(crs1.proj4_init)

fig1, ax1 = plt.subplots(figsize=(7,7), subplot_kw={'projection': crs1})

# useful code to set map extent,
# --- if you want maximum extent, comment out the next line of code ---
ax1.set_extent([-60.14, 130.4, -13.12, -24.59], crs=ccrs.PlateCarree())

# at maximum extent, the circular bound trims map features nicely
ax1.add_geometries(w1['geometry'], crs=crs1, facecolor=facecolor, edgecolor=edgecolor, linewidth=0.5)

# this adds the ocean coloring
ax1.add_feature(cartopy.feature.OCEAN, facecolor=ocean_color, edgecolor='none')

# this adds the cities
c1.plot(ax=ax1, marker='o', color='red', markersize=50)

结果如下:

【问题讨论】:

  • 使用zorder,例如:c1.plot(ax=ax1, marker='o', color='red', markersize=50, zorder=20)应该把所有的点都放在前面。
  • 可以这么简单:)。这成功了。谢谢。

标签: python geopandas cartopy


【解决方案1】:

axes 的默认绘制顺序是补丁、线条、文本。此顺序由 zorder 属性确定。

Polygon/patch,  zorder=1
Line 2D,  zorder=2
Text,  zorder=3

您可以通过设置zorder 来更改各个地图功能的顺序。 任何单独的 plot() 调用都可以为该特定项目的 zorder 设置一个值。

在你的情况下,代码

c1.plot(ax=ax1, marker='o', color='red', markersize=50, zorder=20)

会将标记绘制在所有其他 zorder 小于 20 的特征之上。

Zorder 演示:https://matplotlib.org/gallery/misc/zorder_demo.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    相关资源
    最近更新 更多