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() |
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() |
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 | 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() |
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) |
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.
References
Links | Source |
---|---|
cartopy | cartopy |
always_circular_stereo example | scitools.org.uk |
More advanced mapping with cartopy and matplotlib | scitools.org.uk |