Plotting Random Points with Rivers on Earth using Cartopy

  • Share this:

Code introduction


This function uses the Cartopy library to plot random points on the Earth's surface and adds river features.


Technology Stack : Cartopy, NumPy, Matplotlib

Code Type : Cartopy drawing function

Code Difficulty : Intermediate


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

def plot_earth_with_rivers(arg1, arg2):
    # Create a figure and an axes with Cartopy's projection
    fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
    
    # Generate random latitudes and longitudes
    random_lats = np.random.uniform(-90, 90, arg1)
    random_lons = np.random.uniform(-180, 180, arg2)
    
    # Create a scatter plot of random points on the map
    sc = ax.scatter(random_lons, random_lats, transform=ccrs.PlateCarree())
    
    # Add rivers to the map
    ax.add_feature(cfeature.RIVERS)
    
    # Set the extent of the map
    ax.set_extent([0, 360, -90, 90], crs=ccrs.PlateCarree())
    
    # Set the title of the map
    ax.set_title('Random Points on Earth with Rivers')
    
    # Show the map
    plt.show()