2 · Maps & raster field plots¶
Map is the workhorse. It holds one matplotlib axes, reprojects each input to a chosen display CRS via pyramids (Dataset.to_crs), and renders cleopatra glyphs onto that shared axes — no Cartopy: the projection is applied to the data, not the axes.
The raster field methods all wrap one cleopatra ArrayGlyph and differ only in kind:
| method | renders as |
|---|---|
field |
pixel grid (honours extent) |
contours(filled=True) |
filled contours |
contours() |
line contours |
pcolormesh |
quadrilateral mesh |
block |
discrete cell blocks |
Each call registers a layer; Map.colorbar() then draws a single aggregated colorbar for the layer of interest. Everything below runs on the full-resolution Lisbon DEM.
Setup. Inline plots + resolve the DEM path from the repo root.
%matplotlib inline
from pathlib import Path
# Resolve the repo root so the bundled sample DEM is found whether this runs from
# docs/examples/ (mkdocs) or the repository root.
ROOT = Path.cwd()
while not (ROOT / "examples" / "data" / "LisbonElevation.tif").exists() and ROOT != ROOT.parent:
ROOT = ROOT.parent
DEM = str(ROOT / "examples" / "data" / "LisbonElevation.tif")
print("DEM:", DEM)
DEM: /home/runner/work/Digital-Earth/Digital-Earth/examples/data/LisbonElevation.tif
Read the DEM once; every cell below renders it a different way.
from pyramids.dataset import Dataset
from digitalearth.static import Map
ds = Dataset.read_file(DEM)
field (pixel grid) + an aggregated colorbar¶
field — create a Map in the DEM's own CRS (no reprojection) and draw the raster as a pixel grid. colorbar(label=…) adds one aggregated colorbar for the layer; cmap='terrain' is an elevation-friendly colormap.
m = Map(crs=ds.epsg, figsize=(9, 7))
m.field(ds, cmap='terrain')
m.colorbar(label='elevation (m)')
m.set_title('Lisbon DEM — field')
m.fig;
Filled contours¶
contours(filled=True) — the same data as filled contours. levels=12 sets the number of class breaks; the colorbar becomes discrete to match.
m = Map(crs=ds.epsg, figsize=(9, 7))
m.contours(ds, levels=12, cmap='terrain', filled=True)
m.colorbar(label='elevation (m)')
m.set_title('contourf')
m.fig;
Line contours¶
contours() — line contours (isolines) of elevation. No fill, so no colorbar is needed here.
m = Map(crs=ds.epsg, figsize=(9, 7))
m.contours(ds, levels=15)
m.set_title('contour')
m.fig;
pcolormesh¶
pcolormesh — a quadrilateral mesh; like field but each cell is a true quad (correct for non-uniform/curvilinear grids). block is the same renderer on cell edges.
m = Map(crs=ds.epsg, figsize=(9, 7))
m.pcolormesh(ds, cmap='terrain')
m.colorbar(label='elevation (m)')
m.set_title('pcolormesh')
m.fig;
Reproject to a display CRS (Web Mercator)¶
pyramids does the reprojection; we render on a plain axes (no Cartopy).
Reprojection — set the display CRS to Web Mercator (crs=3857). pyramids reprojects the DEM to that CRS and the axis units become metres; the map is rendered on a plain axes (no Cartopy).
m = Map(crs=3857, figsize=(9, 7))
m.field(ds, cmap='terrain')
m.colorbar(label='elevation (m)')
m.set_title('reprojected to EPSG:3857')
m.fig;
Automatic ECMWF Magics styling¶
Every field above was styled by hand (cmap, levels). For meteorological fields you usually don't
want to — each variable has a canonical operational look. digitalearth.base.autostyle ports ECMWF Magics
identity matching: give it a field's name (or CF standard_name, or units) and it returns the canonical
colormap and contour levels. Map/auto_style consult it automatically, so a recognised field is drawn
the ECMWF way without you specifying anything.
from digitalearth.base.autostyle import magics_style
# Resolve a few fields by name / standard_name / units — each gets a canonical cmap + contour levels.
for ident in [
{"name": "t2m"}, # 2 m temperature, by short name
{"name": "tp"}, # total precipitation
{"standard_name": "air_pressure_at_mean_sea_level"}, # MSLP, by CF standard_name
{"units": "gpm"}, # geopotential height, by units alone
]:
style = magics_style(**ident)
print(f"{ident} -> magics:{style['magics_name']:>4} cmap={style['cmap']:<10} "
f"levels={style['levels'][0]}..{style['levels'][-1]} {style['units']}")
{'name': 't2m'} -> magics: t2m cmap=coolwarm levels=-40..40 celsius
{'name': 'tp'} -> magics: tp cmap=Blues levels=0..100 mm
{'standard_name': 'air_pressure_at_mean_sea_level'} -> magics: msl cmap=Spectral_r levels=960..1052 hPa
{'units': 'gpm'} -> magics: z cmap=viridis levels=4800..6000 gpm