wotaskd
wotaskd

Reputation: 965

Change map boundary color in Cartopy

I'm plotting a world map in Cartopy and as part of the process I want to color all lines white. I have been able to do so for every element with exception of the map's boundaries. This is the code I'm using:

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)

And this is the final result:

enter image description here

Any idea of how to change the boundary line to either white or to turn it off completely, in as few steps as possible?

Thanks!

Upvotes: 8

Views: 6306

Answers (1)

Daniel Kirkham
Daniel Kirkham

Reputation: 377

It is possible to make the frame white with the following line:

ax.outline_patch.set_edgecolor('white')

As an aside, plt.axes accepts a boolean keyword argument frameon, which, when set to False, prevents drawing the border. However, it appears to be ignored by Cartopy. This is a potential bug which should be looked into.

Upvotes: 11

Related Questions