Search code examples
geopandascartopy

Shape disappears after changing the CCRS


Follwing another Q&A, I am plotting the countries of the African continent on the earth sphere:

import cartopy
import cartopy.crs as ccrs
from matplotlib import pyplot as plt

import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
africa = world[(world['continent'] == 'Africa')]

latlon_proj = ccrs.PlateCarree()
axis_proj = ccrs.Mollweide()

ax = plt.axes(projection=axis_proj)
ax.stock_img()

for ea in africa['geometry']:
    
    feat = cartopy.feature.ShapelyFeature(
        [ea],
        latlon_proj,
        facecolor="lime",
        edgecolor='black',
        lw=0.2
        )
    
    ax.add_feature(feat)

plt.show()

enter image description here

However when I change latlon_proj and axis_proj to ccrs.Orthographic(), the African continent disappears:

import cartopy
import cartopy.crs as ccrs
from matplotlib import pyplot as plt

import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
africa = world[(world['continent'] == 'Africa')]

latlon_proj = ccrs.Orthographic()
axis_proj = ccrs.Orthographic()

ax = plt.axes(projection=axis_proj)
ax.stock_img()

for ea in africa['geometry']:
    
    feat = cartopy.feature.ShapelyFeature(
        [ea],
        latlon_proj,
        facecolor="lime",
        edgecolor='black',
        lw=0.2
        )
    
    ax.add_feature(feat)

plt.show()

enter image description here

How can I keep the African continent when changing the CCRS?


Solution

  • There is a wrong use of CRS in the code. Here is the correct version of it.

    import cartopy
    import cartopy.crs as ccrs
    from matplotlib import pyplot as plt
    
    import geopandas as gpd
    
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    africa = world[(world['continent'] == 'Africa')]
    
    latlon_proj = ccrs.PlateCarree()  ## The correct CRS
    axis_proj = ccrs.Orthographic()
    
    ax = plt.axes(projection=axis_proj)
    ax.stock_img()
    
    for ea in africa['geometry']:
    
        feat = cartopy.feature.ShapelyFeature(
            [ea],
            latlon_proj,
            facecolor="lime",
            edgecolor='black',
            lw=0.2
            )
    
        ax.add_feature(feat)
    
    plt.show()
    

    africa