How to Plot a Heatmap Over Polar Regions in Python Using Cartopy and Matplotlib ?

Published: September 01, 2022

Updated: September 19, 2024

Tags: Python; Matplotlib; Cartopy;

DMCA.com Protection Status

Introduction

When working with geospatial data, it's common to plot maps in polar regions like the Arctic or Antarctica. This guide shows how to create heatmaps over polar regions using cartopy, Matplotlib, and Python. Cartopy is particularly useful for geospatial data visualization because it supports projections and can handle geographic features with ease.

Plotting a Heatmap Over Antarctica Using Cartopy

Basic map of Antarctica

This example demonstrates how to plot a basic map of Antarctica and overlay a circular boundary to emphasize the southernmost polar region.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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()

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

Explanation:
- South Polar Stereographic projection (ccrs.SouthPolarStereo) is used because it accurately represents polar regions.
- set_extent([-180, 180, -90, -60]) limits the plot to latitudes below -60 degrees, focusing on Antarctica.
- Circular boundary: This is achieved using matplotlib.path, ensuring the plot has a smooth boundary resembling the circular shape of polar projections.
- Adding features: Cartopy's add_feature method helps in adding geographic features like land and ocean to the plot.

Note: You can also create a simpler version of the map without the circular boundary, which still effectively visualizes the polar region. This approach simplifies the code by focusing only on setting the map's extent and basic features like land, ocean, and gridlines.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import cartopy.feature
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

# Create a figure with the South Polar Stereographic projection
fig = plt.figure(figsize=[10, 5])
ax1 = plt.subplot(1, 2, 1, projection=ccrs.SouthPolarStereo())

# Set the map extent to cover latitudes below -60 degrees, focusing on Antarctica
ax1.set_extent([-180, 180, -90, -60], ccrs.PlateCarree())

# Add geographic features for context: land and ocean
ax1.add_feature(cartopy.feature.LAND, zorder=0)
ax1.add_feature(cartopy.feature.OCEAN, zorder=0)

# Display gridlines to provide reference points for longitude and latitude
ax1.gridlines()

# Save the figure with high resolution and display it
plt.savefig("cartopy_antarctica_02.png", bbox_inches='tight', dpi=200)
plt.show()

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

Explanation:
- Simplified map: This version excludes the circular boundary, making the code easier to follow and useful when a full projection of the polar region is sufficient.
- Extent: The map focuses on the polar region by setting the extent from -180 to 180 degrees longitude and -90 to -60 degrees latitude.
- Geographic features: Cartopy's built-in LAND and OCEAN features provide a simple yet effective geographic context.
- Gridlines: Adding gridlines offers useful references for the map's spatial orientation.

This simplified approach is great for cases where you want a straightforward visual representation of Antarctica without needing to emphasize specific regions or apply custom boundaries.

Plotting a Heatmap Over Antarctica Using Real Data (example 1)

In this example, we load real geospatial data (from a MODIS file) and plot it as a heatmap over Antarctica. The focus here is to visualize geospatial data using Cartopy's imshow for raster plotting.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from cartopy import config
from matplotlib.pyplot import figure

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib.path as mpath
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()

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

Explanation:
- Loading data: The heatmap is created using data from a file, such as MODIS remote sensing data. The data is visualized as a 2D grid over Antarctica.
- imshow function: This function plots the loaded data onto the map. extent defines the bounding box for the image, and transform=ccrs.PlateCarree() aligns the grid data with the geographic coordinates.
- Color map: The 'jet' colormap is used for the heatmap, which can be adjusted depending on the data range (vmin, vmax).

Plotting a Heatmap Over Antarctica Using Real Data (example 2)

This is another variant of heatmap plotting with Cartopy. Similar to example 1, but with simpler data and without the circular boundary.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

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

Explanation:
This version follows the same logic but without the circular cropping, making it ideal for simpler visualizations.
Heatmap over Antarctica using cartopy.

Final Comments

  • Projections: The choice of projection, such as SouthPolarStereo, is essential when working with polar data to avoid distortions.
  • Boundaries: Using set_boundary() provides control over how much of the map is displayed, allowing for focusing on circular regions like the polar zones.
  • Heatmaps: Cartopy, in combination with Matplotlib's imshow(), provides a flexible way to visualize geospatial data. Experiment with colormaps (cmap) and adjust the vmin and vmax values depending on your data range.

This guide should help you get started with visualizing polar data and creating heatmaps over Antarctica using Cartopy and Matplotlib.

Plotting a Heatmap Over Arctica Using Cartopy

This guide demonstrates how to plot a heatmap over the Arctic region using Python libraries such as Cartopy and Matplotlib. We will cover how to create a basic map projection of the Arctic and further customize it to fit a circular boundary using the North Polar Stereographic projection.

Basic map of Arctica

This example introduces the creation of a basic Arctic map using Cartopy's NorthPolarStereo projection, which is designed to accurately represent the polar regions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import cartopy.feature
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import numpy as np

# Create figure and axis using North Polar Stereographic projection
fig = plt.figure(figsize=[10, 5])
ax1 = plt.subplot(1, 2, 1, projection=ccrs.NorthPolarStereo())

# Set map extent: focusing on latitudes between 60 and 90 degrees (the Arctic region)
ax1.set_extent([-180, 180, 60, 90], ccrs.PlateCarree())

# Add coastlines for geographic context
ax1.coastlines()

# Add gridlines to show latitude and longitude markers
ax1.gridlines()

# Define a circular boundary to crop the map within a circular region
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)

# Apply the circular boundary to the polar map to focus on the Arctic region
ax1.set_boundary(circle, transform=ax1.transAxes)

# Add title
plt.title('How to Plot a Heatmap Over Polar Regions in Python \n Using Cartopy and Matplotlib ?', 
          fontsize=10)

# Save the figure as a high-resolution PNG file and display it
plt.savefig("cartopy_arctica_01.png", bbox_inches='tight', dpi=200)
plt.show()

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

Explanation:
- Projection: The NorthPolarStereo projection is used for mapping polar regions, ensuring accuracy in the high-latitude Arctic region.
- Extent: The map covers longitudes from -180 to 180 degrees and latitudes from 60 to 90 degrees, capturing the Arctic.
- Geographic Features: Adding coastlines and gridlines provides helpful context for interpreting the map's spatial information.
- Circular Boundary: The map is cropped using a circular boundary, focusing attention on the region around the North Pole.

Simplified Arctic Map Without Circular Boundary:

If you prefer a simpler version without the circular boundary, the following code provides a straightforward approach:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import cartopy.feature

import matplotlib.path as mpath
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

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

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

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

ax1.coastlines()

# Add title
plt.title('How to Plot a Heatmap Over Polar Regions in Python \n Using Cartopy and Matplotlib ?', 
          fontsize=10)

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

plt.show()

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

This version provides a basic map of the Arctic without the use of a circular boundary, making it easier to implement and visualize the polar region in a straightforward manner.

Plotting a Heatmap Over Arctica Using Real Data (example 1)

In this example, we load real geospatial data (from a MODIS file) and plot it as a heatmap over Arctica. The focus here is to visualize geospatial data using Cartopy's imshow for raster plotting.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from cartopy import config
from matplotlib.pyplot import figure

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import numpy as np

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

ax = plt.axes(projection=ccrs.NorthPolarStereo())

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

im = ax.imshow(data, extent=[-180,180,-90,90], transform=ccrs.PlateCarree(),cmap='jet', alpha=0.6)

ax.coastlines()

plt.colorbar(im)

plt.title('How to Plot a Heatmap Over Polar Regions in Python \n Using Cartopy and Matplotlib ?', 
          fontsize=10)

# Save the figure as a high-resolution PNG file and display it
plt.savefig("cartopy_arctica_03.png", bbox_inches='tight', dpi=200)

plt.show()

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

Plotting a Heatmap Over Arctica Using Real Data (example 2)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from cartopy import config
from matplotlib.pyplot import figure

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

# Create figure and axis using North Polar Stereographic projection
fig = plt.figure(figsize=[10, 5])
ax1 = plt.subplot(1, 2, 1, projection=ccrs.NorthPolarStereo())

# Set map extent: focusing on latitudes between 60 and 90 degrees (the Arctic region)
ax1.set_extent([-180, 180, 60, 90], ccrs.PlateCarree())

# Add coastlines for geographic context
ax1.coastlines()

# Add gridlines to show latitude and longitude markers
ax1.gridlines()

# Define a circular boundary to crop the map within a circular region
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)

# Apply the circular boundary to the polar map to focus on the Arctic region
ax1.set_boundary(circle, transform=ax1.transAxes)

# Add title
plt.title('How to Plot a Heatmap Over Polar Regions in Python \n Using Cartopy and Matplotlib ?', 
          fontsize=10)

im = ax1.imshow(data, extent=[-180,180,-90,90], transform=ccrs.PlateCarree(),cmap='jet', alpha=0.6)

# Save the figure as a high-resolution PNG file and display it
plt.savefig("cartopy_arctica_04.png", bbox_inches='tight', dpi=200)
plt.show()

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

References

Image

of