You can download this code by clicking the button below.
This code is now available for download.
This function generates a world map using the Cartopy and Matplotlib libraries. It randomly selects the map projection type and adds geographic features.
Technology Stack : Cartopy, Matplotlib
Code Type : Map drawing
Code Difficulty : Intermediate
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
def plot_world_map(projection_type='mill'):
"""
This function generates a world map using Cartopy and Matplotlib.
It randomly selects the projection type and adds geographic features.
"""
# Set up the map projection and create a figure and axis
fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()})
# Randomly select the projection type
if projection_type == 'mill':
ax.set_projection(ccrs.Miller())
elif projection_type == 'robin':
ax.set_projection(ccrs.Robinson())
elif projection_type == 'merc':
ax.set_projection(ccrs.Mercator())
else:
raise ValueError("Unsupported projection type")
# Add geographic features to the map
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.add_feature(cfeature.OCEAN, facecolor='lightblue')
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
# Set map limits
ax.set_global()
# Display the map
plt.show()