Interactive plots using hvplot

Interactive plots using hvplot#

Xarray’s builtin plotting functionality wraps matplotlib.

The holoviews ecosystem provides the hvplot package to allow easy visualization of xarray (and other) objects. These plots build on Bokeh.

hvplot makes uses of xarray’s accessor interface. This means that all xarray objects gain a .hvplot attribute that lets you access .hvplot functionality as easily as you would use .plot. All you need to do is import hvplot.xarray

For more, see hvplot’s documentation

import cartopy.crs as ccrs
import hvplot.xarray
import xarray as xr
ds = xr.tutorial.open_dataset("air_temperature.nc").rename({"air": "Tair"})

Basics#

hvplot makes the same default choices as DataArray.plot

ds.Tair.hvplot()
# 2D array yields a quadmesh plot
ds.Tair.isel(time=1).hvplot(cmap="fire")
# 1D array yields a line plot
ds.Tair.isel(time=1, lon=1).hvplot()

Interactivity#

But hvplot shines when interactivity is used. Here we can give it all the data and ask it to create a nice slider to control the time slice using the groupby kwarg.

ds.Tair.hvplot(
    groupby="time",
    clim=(250, 295),  # adds a widget for time  # sets colorbar limits
)

Animations#

are easy.

# set constant colorbar limits
ds.Tair.hvplot(
    groupby="time",  # adds a widget for time
    clim=(250, 295),  # sets colormap limits
    widget_type="scrubber",
    widget_location="bottom",
)

Geography#

ds.Tair.isel(time=1).hvplot(
    projection=ccrs.Orthographic(-90, 30),
    coastline=True,
)