Plotting Coastlines and Marking Points with Basemap

  • Share this:

Code introduction


This function uses the Basemap library to plot coastlines on a map and mark a point.


Technology Stack : Basemap, NumPy, Matplotlib

Code Type : Geographic Information Visualization

Code Difficulty : Intermediate


                
                    
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

def plot_coastlines(arg1, arg2):
    """
    This function uses Basemap to plot coastlines on a map.
    
    :param arg1: The longitude of the center of the map
    :param arg2: The latitude of the center of the map
    """
    # Create a figure and an axes
    fig, ax = plt.subplots()
    
    # Create a Basemap instance
    m = Basemap(projection='merc', llcrnrlat=-60, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180, lat_ts=20, resolution='c')
    
    # Draw coastlines
    m.drawcoastlines()
    
    # Plot a point
    lon, lat = arg1, arg2
    x, y = m(lon, lat)
    m.plot(x, y, marker='o', color='red')
    
    # Display the map
    plt.show()