Cartopy Map with Custom Longitude Range

  • Share this:

Code introduction


This function uses the Cartopy library to draw a map and adds features such as coastlines, borders, land, ocean, and rivers. Users can specify the longitude range of the map.


Technology Stack : Cartopy, Matplotlib

Code Type : Map drawing

Code Difficulty : Intermediate


                
                    
def draw_map_with_countries(arg1, arg2):
    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=':')
    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.OCEAN)
    ax.add_feature(cfeature.RIVERS)

    ax.set_extent([arg1, arg2], crs=ccrs.PlateCarree())
    plt.show()