How to plot a heatmap over polar regions using cartopy, matplotlib and python ?

Created September 01, 2022 | Viewed 295 | by Benjamin

Examples of how to plot a heatmap over polar regions using cartopy, matplotlib and python:

Plot a heatmap over antarctica using cartopy (example 1)

````import cartopy.feature`

`import matplotlib.path as mpath`

`fig = plt.figure(figsize=[10, 5])`

`ax1 = plt.subplot(1, 2, 1, projection=ccrs.SouthPolarStereo())`

`# Limit the map to -60 degrees latitude and below.`
`ax1.set_extent([-180, 180, -90, -60], ccrs.PlateCarree())`

`ax1.add_feature(cartopy.feature.LAND)`
`ax1.add_feature(cartopy.feature.OCEAN)`

`ax1.gridlines()`

`theta = np.linspace(0, 2*np.pi, 100)`
`center, radius = [0.5, 0.5], 0.5`
`verts = np.vstack([np.sin(theta), np.cos(theta)]).T`
`circle = mpath.Path(verts * radius + center)`

`ax1.set_boundary(circle, transform=ax1.transAxes)`

`plt.savefig("cartopy_antarctica_01.png", bbox_inches='tight', dpi=200)`

`plt.show()`
```

````from cartopy import config`
`from matplotlib.pyplot import figure`

`import cartopy.crs as ccrs`
`import matplotlib.pyplot as plt`
`import numpy as np`

`data = np.loadtxt('modis_myd06_cpop_2d_hist_1b1_grid.txt')`

`fig = plt.figure(num=None, figsize=(8, 6), dpi=80, edgecolor='k')`

`ax = plt.axes(projection=ccrs.SouthPolarStereo())`

`ax.set_extent([-180, 180, -90, -45], ccrs.PlateCarree())`

`theta = np.linspace(0, 2*np.pi, 200)`
`center, radius = [0.5, 0.5], 0.5`
`verts = np.vstack([np.sin(theta), np.cos(theta)]).T`
`circle = mpath.Path(verts * radius + center)`

`ax.set_boundary(circle, transform=ax.transAxes)`

`ax.imshow(data.T, origin='lower', extent=[-180,180,-90,90], transform=ccrs.PlateCarree(),cmap='jet',vmin=0, vmax=1.0)`

`ax.coastlines()`

`plt.title("Plot a heatmap with cartopy in python", fontsize=12)`

`plt.savefig("cartopy_antarctica_03.png", bbox_inches='tight', dpi=200)`

`plt.show()`
```

Plot a heatmap over antarctica using cartopy (example 2)

````import cartopy.feature`

`import matplotlib.path as mpath`

`fig = plt.figure(figsize=[10, 5])`

`ax1 = plt.subplot(1, 2, 1, projection=ccrs.SouthPolarStereo())`

`ax1.set_extent([-180, 180, -90, -60], ccrs.PlateCarree())`

`ax1.add_feature(cartopy.feature.LAND)`
`ax1.add_feature(cartopy.feature.OCEAN)`

`ax1.gridlines()`

`plt.savefig("cartopy_antarctica_02.png", bbox_inches='tight', dpi=200)`

`plt.show()`
```

````from cartopy import config`
`from matplotlib.pyplot import figure`

`import cartopy.crs as ccrs`
`import matplotlib.pyplot as plt`
`import numpy as np`

`data = np.loadtxt('modis_myd06_cpop_2d_hist_1b1_grid.txt')`

`fig = plt.figure(num=None, figsize=(8, 6), dpi=80, edgecolor='k')`

`ax = plt.axes(projection=ccrs.SouthPolarStereo())`

`ax.set_extent([-180, 180, -90, -60], ccrs.PlateCarree())`

`ax.imshow(data.T, origin='lower', extent=[-180,180,-90,90], transform=ccrs.PlateCarree(),cmap='jet',vmin=0, vmax=1.0)`

`ax.coastlines()`

`plt.title("Plot a heatmap with cartopy in python", fontsize=12)`

`plt.savefig("cartopy_antarctica_04.png", bbox_inches='tight', dpi=200)`
```