Plotting Country Maps with Cartopy

  • Share this:

Code introduction


This function uses the Cartopy library to plot maps of a list of specified countries. It takes a list of countries as an argument and displays their borders and coastlines on the map.


Technology Stack : Cartopy, Matplotlib

Code Type : Map drawing function

Code Difficulty : Intermediate


                
                    
def plot_countries_countries(country_list):
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(cfeature.BORDERS, linestyle=':')

    for country in country_list:
        ax.add_feature(cfeature.NATURAL_EARTH, name=country)

    plt.show()