Timeseries Data#

Almost all the other sections in the user guide mention timeseries. This section demonstrates the special functionality that hvPlot provides specifically for dealing with time.

import hvplot.pandas  # noqa
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature as sst

sst.hvplot()

By default, the index will be used as the x-axis when plotting tabular data. If the index is composed of datetimes, and they are not in chronological order, hvPlot will try to sort them before plotting (unless you set sort_date=False).

scrampled = sst.sample(frac=1)
scrampled.hvplot()

Tickers#

The datetime tickers will be set to a default that is meant to fit in the allotted space. If you’d rather use a different format, then you can declare an explicit date ticker according to the rules on Bokeh DatetimeTickFormatter.

from bokeh.models.formatters import DatetimeTickFormatter

formatter = DatetimeTickFormatter(months='%b %Y')
sst.hvplot(xformatter=formatter)

Auto-range#

(Available with HoloViews >= 1.16)

Automatic ranging, aka auto-ranging, on the data in x or y is supported, making it easy to scale the given axes and fit the entire visible curve after a zoom or pan. Try zooming in on the plot and panning around, the y range nicely adapt to fit the curve.

sst.hvplot(autorange="y")

Pandas datetime features#

hvPlot takes advantage of datetime features to make it trivial to produce plots that are aggregated on some feature of the date. For instance in the case of temperature data, it might be interesting to examine the monthly temperature distribution. We can easily do that by setting by='index.month'.

sst.hvplot.violin(by='index.month')

We can also use these datetime features as the x and y. Here we’ll look at the mean temperature at each hour of the day for each month in our dataset.

sst.hvplot.heatmap(x='index.hour', y='index.month', C='temperature', cmap='reds')
WARNING:param.HeatMapPlot02461: HeatMap element index is not unique,  ensure you aggregate the data before displaying it, e.g. using heatmap.aggregate(function=np.mean). Duplicate index values have been dropped.

Combining this with the information from the section on widgets, we can even use the datetime features to produce a plot that steps through each month in the data.

sst.hvplot(groupby=['index.year', 'index.month'], widget_type='scrubber', widget_location='bottom')

Xarray datetime features#

The same datetime features can be used with xarray data as well, although for now, the functionality is only supported for non-gridded output.

import xarray as xr
import hvplot.xarray  # noqa

air_ds = xr.tutorial.open_dataset('air_temperature').load()
air_ds
<xarray.Dataset>
Dimensions:  (lat: 25, time: 2920, lon: 53)
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
Data variables:
    air      (time, lat, lon) float32 241.2 242.5 243.5 ... 296.5 296.2 295.7
Attributes:
    Conventions:  COARDS
    title:        4x daily NMC reanalysis (1948)
    description:  Data is from NMC initialized reanalysis\n(4x/day).  These a...
    platform:     Model
    references:   http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...

Similar to how we did for sea surface temperature above, we can get the distribution of air temperature by month.

air_ds.hvplot.violin(y='air', by='time.month')

Once we reduce the dimensionality (by taking the mean over ‘lat’ and ‘lon’), we can groupby various datetime features.

air_ds.mean(dim=['lat', 'lon']).hvplot(by='time.hour', groupby=['time.year', 'time.month'])

Note that xarray supports grouping and aggregation using a similar syntax. To learn more about timeseries in xarray, see the xarray timeseries docs.

Downsample time series#

(Available with HoloViews >= 1.16)

An option when working with large time series is to downsample the data before plotting it. This can be done with downsample=True, which applies the lttb (Largest Triangle Three Buckets) algorithm to the data.

sst.hvplot(label="original") * sst.hvplot(downsample=True, label="downsampled")
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Right click to download this notebook from GitHub.