Search code examples
pythongeopandascartopy

Geopandas world map in Polar Stereographic projection with coloured oceans


Adding a further requirement to this question, I also need to have the oceans in blue (or any other colour).

For the 'PlateCarree' projection I can simply do this

crs = ccrs.PlateCarree()
crs_proj4 = crs.proj4_init
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
w = world.to_crs(crs_proj4)
g = w.plot(facecolor='sandybrown', edgecolor='black')

And now adding the ocean colour

g.set_facecolor('#A8C5DD')

If I now want to use a polar stereographic peojection

ccrs.NorthPolarStereo()

or

ccrs.SouthPolarStereo()

the projection does not work. When applying the answer to this question, I cannot get the oceans coloured


Solution

  • You need to plot the map geometries on Cartopy geoaxes, and use cartopy.feature.OCEAN to plot the ocean. Here is the working code that you may try. Read the comments in the code for clarification.

    import geopandas as gpd
    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import cartopy
    
    facecolor = 'sandybrown'
    edgecolor = 'black'
    ocean_color = '#A8C5DD'
    
    #crs1 = ccrs.SouthPolarStereo()
    crs1 = ccrs.NorthPolarStereo()
    
    world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    w1 = world.to_crs(crs1.proj4_init)
    
    fig1, ax1 = plt.subplots(figsize=(7,7), subplot_kw={'projection': crs1})
    
    # useful code to set map extent,
    # --- if you want maximum extent, comment out the next line of code ---
    ax1.set_extent([-60.14, 130.4, -13.12, -24.59], crs=ccrs.PlateCarree())
    
    # at maximum extent, the circular bound trims map features nicely
    ax1.add_geometries(w1['geometry'], crs=crs1, \
                    facecolor=facecolor, \
                    edgecolor=edgecolor, \
                    linewidth=0.5)
    
    # this adds the ocean coloring
    ax1.add_feature(cartopy.feature.OCEAN, facecolor=ocean_color, edgecolor='none')
    
    plt.show()
    

    The output plot will be:

    enter image description here