Cartopy: Drawing the coastlines with a country border removed

Viewed 4441

I want to draw the outline of China in one color whilst also showing the global coastlines in another. My first attempt at doing this was as follows:

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


countries = shapereader.natural_earth(resolution='110m',
                                      category='cultural',
                                      name='admin_0_countries')

# Find the China boundary polygon.
for country in shapereader.Reader(countries).records():
    if country.attributes['su_a3'] == 'CHN':
        china = country.geometry
        break
else:
    raise ValueError('Unable to find the CHN boundary.')


plt.figure(figsize=(8, 4))
ax = plt.axes(projection=ccrs.PlateCarree())

ax.set_extent([50, 164, 5, 60], ccrs.PlateCarree())

ax.add_feature(feature.LAND)
ax.add_feature(feature.OCEAN)
ax.add_feature(feature.COASTLINE, linewidth=4)

ax.add_geometries([china], ccrs.Geodetic(), edgecolor='red',
                  facecolor='none')

plt.show()

The result

I've made the coastlines thick so that you can see the fact that they are overlapping with the country border.

My question is: Is there a way to remove the coastline next to the country outline so that I don't have the two lines interacting with one-another visually?

Note: This question was asked to me directly via email, and I chose to post my response here so that others may learn/benefit from a solution.

1 Answers
Related