Active October 13, 2021 / Viewed 932 / Comments 0 / Edit

Examples of how to plot a heatmap on a global map with cartopy in python

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):

`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)`

`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)`

- Using cartopy with matplotlib
- More advanced mapping with cartopy and matplotlib
- Installation
- Download data from Natural Earth and OpenStreetMap for Cartopy
- Natural Earth S3 Links
- Download data from Natural Earth and OpenStreetMap for Cartopy
- Understanding the transform and projection keywords
- Visualizing Satellite Data Using Matplotlib and Cartopy
- Coordinate reference systems in Cartopy

Ads

License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Ads

Table of contents

Get gridded data
Plot a simple map with cartopy
Plot a heatmap with cartopy
References
Ads