Creating Maps with Circles using Cartopy in Python

  • Share this:

Code introduction


This function creates a map using the Cartopy library and draws multiple circles on it. The positions and radii of these circles are specified through parameters.


Technology Stack : Cartopy, NumPy, Matplotlib

Code Type : Geographic Information Visualization

Code Difficulty : Intermediate


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

def draw_map_with_circles(arg1, arg2):
    # Create a figure and an axis
    fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
    
    # Add coastlines and countries
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(cfeature.BORDERS, linestyle=':')
    
    # Create circles at specified latitudes and longitudes
    for lat, lon, radius in arg1:
        circle = plt.Circle((lon, lat), radius, color='red', fill=False)
        ax.add_patch(circle)
    
    # Set the limits of the map
    ax.set_xlim(arg2[0], arg2[1])
    ax.set_ylim(arg2[2], arg2[3])
    
    # Show the map
    plt.show()