Highlight Country Boundaries on World Map

  • Share this:

Code introduction


This function uses the Basemap library to draw a world map and highlight the boundaries of a specified country.


Technology Stack : Basemap, Numpy, Matplotlib

Code Type : Function

Code Difficulty : Intermediate


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

def plot_world_map_with_country_boundaries(country_code):
    # Initialize the map
    m = Basemap(projection='mill',llcrnrlat=-60,urcrnrlat=90, \
                llcrnrlon=-180,urcrnrlon=180,resolution='c')
    
    # Draw the coastlines, countries, and fill continents
    m.drawcoastlines()
    m.fillcontinents(color='coral',lake_color='aqua')
    m.drawcountries()
    
    # Plot the selected country based on its country code
    m.plot(-100, 100, marker='o', color='red', markersize=10)
    plt.text(-100, 100, country_code, fontsize=12, ha='center', va='center')
    
    # Display the map
    plt.show()

# Example usage:
# plot_world_map_with_country_boundaries('US')