Examples of how to plot a heatmap on a global map with cartopy in python
Table of contents
Get gridded data
Let's first get some data:
import numpy as np
data = np.loadtxt('https://raw.githubusercontent.com/benjamin-hg-marchant/teaching/main/datasets/modis_myd06_cpop_2d_hist_1b1_grid.txt')
Note that
data.shape
gives
(360, 180)
Here longitude [-180,180] are on the first axis (axis =0) and latitude [-90: 90] on the second axis (axis =1):
Plot a simple map with cartopy
from cartopy import config
from matplotlib.pyplot import figure
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
fig = plt.figure(num=None, figsize=(8, 6), dpi=80, edgecolor='k')
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.title("Plot a global map with cartopy in python", fontsize=12)
plt.savefig("cartopy_heatmap_01.png", bbox_inches='tight', dpi=200)
Plot a heatmap with cartopy
from cartopy import config
from matplotlib.pyplot import figure
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
fig = plt.figure(num=None, figsize=(8, 6), dpi=80, edgecolor='k')
ax = plt.axes(projection=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_heatmap_02.png", bbox_inches='tight', dpi=200)