Search code examples
pythoncartopy

cartopy doesnt fill country even though fillcolor is set


I would like to highlight a country by filling it with the color red. However, when I do so following examples such as this one: https://matthewkudija.com/blog/2018/05/25/country-maps/

It doesn't work. Instead, it just plots the original color. It only seems to work when I use the PlateCarree projection.

import cartopy
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 10))

proj = ccrs.Orthographic(50, 30)
ax = plt.axes(projection=proj)

ax.add_feature(cartopy.feature.OCEAN, zorder=0)
ax.add_feature(cartopy.feature.LAND, zorder=0, edgecolor='black')

ax.set_global()
ax.gridlines()
ax.add_feature(cartopy.feature.BORDERS, linestyle=':', alpha=1)

shpfilename = shpreader.natural_earth(resolution='110m',
                                      category='cultural',
                                      name='admin_0_countries')
reader = shpreader.Reader(shpfilename)
countries = reader.records()

for country in countries:
    if country.attributes['ADM0_A3'] == 'OMN':
        break
ax.add_geometries(country.geometry, proj,
                  # facecolor=(1, 0, 0),
                  facecolor='red',
                  # label=country.attributes['adm0_a3'])
                  label=country.attributes['ADM0_A3']
                 ,zorder=20)


Solution

  • Is there a specific reason you need the crs to be set to Orthographic? As you said, the code ran successfully for me using

    import cartopy
    import cartopy.crs as ccrs
    from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
    import cartopy.io.shapereader as shpreader
    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots(figsize=(10, 10))
    
    proj = ccrs.Orthographic(50, 30)
    ax = plt.axes(projection=proj)
    
    ax.add_feature(cartopy.feature.OCEAN, zorder=0)
    ax.add_feature(cartopy.feature.LAND, zorder=0, edgecolor='black')
    
    ax.set_global()
    ax.gridlines()
    ax.add_feature(cartopy.feature.BORDERS, linestyle=':', alpha=1)
    
    shpfilename = shpreader.natural_earth(resolution='110m',
                                          category='cultural',
                                          name='admin_0_countries')
    reader = shpreader.Reader(shpfilename)
    countries = reader.records()
    
    for country in countries:
        if country.attributes['ADM0_A3'] == 'OMN':
            ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor='red')
    

    enter image description here