Creating a Global Map with Country Boundaries Using Cartopy

  • Share this:

Code introduction


This function uses the Cartopy library to draw a global map that includes country boundaries.


Technology Stack : Cartopy, GeoAxes, PlateCarree, BORDERS, cfeature

Code Type : Function

Code Difficulty : Intermediate


                
                    
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np

def plot_world_map_with_countries():
    # Create a figure and an axis with Cartopy's GeoAxes
    fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
    
    # Add countries to the map
    ax.add_feature(cfeature.BORDERS, linestyle=':')
    
    # Set the extent of the map to cover the entire world
    ax.set_extent([-180, 180, -90, 90], crs=ccrs.PlateCarree())
    
    # Display the map
    plt.show()