Introduction to contour lines¶
This notebook introduces the two main ways to draw contour lines in earthkit-plots:
ekp.geo.contour– the high-level, single-call APIMap.contour– the lower-level API that gives you full control over the map and lets you layer multiple plots
[1]:
import earthkit.data as ekd
import earthkit.plots as ekp
data = ekd.from_source("sample", "era5-2t-msl-1985122512.grib").to_fieldlist()
pressure = data.sel({"parameter.variable": "msl"})
temperature = data.sel({"parameter.variable": "2t"})
High-level API: ekp.geo.contour¶
The simplest way to draw contour lines is with ekp.geo.contour. By default it uses the plasma colour map and automatically picks sensible contour levels.
[2]:
ekp.geo.contour(pressure, units="hPa", domain="Europe")
[2]:
<earthkit.plots.components.maps.Map at 0x12f6a9a90>
Automatic styles with style="auto"¶
For well-known meteorological variables earthkit-plots has pre-defined styles. Pass style="auto" and it will select the appropriate one automatically.
[3]:
ekp.geo.contour(pressure, units="hPa", style="auto", domain="Europe")
[3]:
<earthkit.plots.components.maps.Map at 0x16b188d90>
ekp.geo.plot does the same thing but also chooses the method (contour, filled contour, etc.) automatically:
[4]:
ekp.geo.plot(pressure, units="hPa")
[4]:
<earthkit.plots.components.maps.Map at 0x16805eb50>
Lower-level API: Map.contour¶
Map.contour gives you more control. You create the map first, then add layers to it. This makes it easy to combine contour lines with other plot types.
[5]:
chart = ekp.Map(domain="Europe")
chart.contour(pressure, units="hPa")
chart.land()
chart.coastlines()
chart.title()
chart.show()
Overlaying contour lines on a filled map¶
Because Map.contour and Map.contourf are separate calls you can freely combine them on one map.
[6]:
chart = ekp.Map(domain="Europe")
chart.contourf(temperature, units="celsius", style="auto")
chart.contour(pressure, units="hPa", style="auto")
chart.legend()
chart.land()
chart.coastlines()
chart.title()
chart.show()
[7]:
ekp.geo.plot(data, groupby="time.valid_datetime")
[7]:
<earthkit.plots.components.maps.Map at 0x16a927cd0>
[8]:
style = ekp.styles.Style(
units="celsius",
levels=range(-30, 31, 2),
extend="both",
cmap="turbo",
)
ekp.geo.plot(temperature, domain="Europe", style=style)
[8]:
<earthkit.plots.components.maps.Map at 0x16bc00d90>
What’s next?¶
The next notebook covers how to control contour levels, line widths and line styles.