Python matplotlib example maps#

Software requirements:

  • Python 3

  • matplotlib

  • cartopy

Run the map example script:

python matplotlib_map.py

Script matplotlib_map.py#

"""
DKRZ matplotlib script: matplotlib_map.py

     - Global maps
     - projections: PlateCarree, Orthographic
     - states

 08.02.21 meier-fleischer(at)dkrz.de

"""
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

#-----------------------------------------------------------------------
#-- Function: main
#-----------------------------------------------------------------------
def main():

    fig = plt.figure(figsize=(10, 10))
    ax1 = fig.add_subplot(2,1,1, projection=ccrs.PlateCarree())
    ax2 = fig.add_subplot(2,1,2, projection=ccrs.Orthographic(central_latitude=50.0,
                                                           central_longitude=10.0))
    states_provinces = cfeature.NaturalEarthFeature(category='cultural',
                                name='admin_1_states_provinces_lines',
                                scale='50m', facecolor='none')
    ax1.set_global()
    ax1.coastlines()
    ax1.add_feature(cfeature.LAND, zorder=0, color='coral')
    ax1.add_feature(cfeature.BORDERS, linewidth=0.2, edgecolor='black')
    ax1.add_feature(states_provinces, linewidth=0.2, edgecolor='black')
    ax1.gridlines(linewidth=0.5, color='gray',
                  xlocs=range(-180,180,30), ylocs=range(-90,90,30))

    ax2.set_global()
    ax2.coastlines()
    ax2.add_feature(cfeature.LAND, zorder=0, color='coral')
    ax2.add_feature(cfeature.BORDERS, linewidth=0.2, edgecolor='black')
    ax2.add_feature(states_provinces, linewidth=0.2, edgecolor='black')
    ax2.gridlines(linewidth=0.5, color='gray',
                  xlocs=range(-180,180,30), ylocs=range(-90,90,30))

    plt.savefig('plot_matplotlib_map_bbox_2.png', bbox_inches='tight', dpi=100)

if __name__ == '__main__':
    main()

Plot result#

Matplotlib maps 1 example w400

Matplotlib maps 2 example w400