在Cartopy中更改地图边界颜色

wot*_*skd 5 python matplotlib cartopy

我在Cartopy中绘制世界地图,作为整个过程的一部分,我想将所有线条涂成白色.除了地图的边界外,我已经能够为每个元素做到这一点.这是我正在使用的代码:

import matplotlib.pyplot as plt
import cartopy
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs

fig = plt.Figure()
fig.set_canvas(plt.gcf().canvas)

ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, linewidth=0.5, edgecolor='white')
ax.set_extent([-150,60,-25,60])

shpf = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')

reader = shpreader.Reader(shpf)
countries = reader.records()

for country in countries:
    ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0,1,0), linewidth=0.5, edgecolor='white', label=country.attributes['adm0_a3'])

fig.savefig("final_image.png", format='png', dpi=200)
Run Code Online (Sandbox Code Playgroud)

这是最终的结果:

在此输入图像描述

想知道如何将边界线改为白色或完全关闭,尽可能少的步骤?

谢谢!

小智 7

可以使用以下行使框架变为白色:

ax.outline_patch.set_edgecolor('white')
Run Code Online (Sandbox Code Playgroud)

plt.axes顺便说一句,接受一个布尔关键字参数frameon,当设置为时,它会False阻止绘制边框.然而,它似乎被Cartopy忽略了.这是一个潜在的错误,应该加以研究.

  • 啊,打败我吧!你也可以使用`ax.outline_patch.set_visible(False)`. (3认同)
  • `ax.spines['geo'].set_edgecolor('white')`。您的解决方案已被弃用 (2认同)