Plotting a Map with Specified Coordinates using Basemap

  • Share this:

Code introduction


This function uses the Basemap library to draw a map and marks a point on the map with the specified longitude and latitude.


Technology Stack : Basemap, Matplotlib

Code Type : Map drawing

Code Difficulty : Intermediate


                
                    
def plot_map_with_basemap(longitude, latitude):
    """
    This function plots a map using Basemap library and marks a point with given longitude and latitude.
    """
    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt

    # Create a map object
    m = Basemap(projection='merc', llcrnrlat=-60, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180, lat_ts=20, resolution='c')
    
    # Draw coastlines, countries, and meridians
    m.drawcoastlines()
    m.drawcountries()
    m.drawmeridians(np.arange(-180, 181, 30), labels=[1, 0, 0, 0])
    
    # Draw parallels
    m.drawparallels(np.arange(-90, 91, 10), labels=[1, 0, 0, 0])
    
    # Plot the point
    x, y = m(longitude, latitude)
    m.plot(x, y, 'ro', markersize=10)
    
    # Show the map
    plt.show()