year-vs-climatology / hvplot_docs /Timeseries_Data.md
ahuang11's picture
Upload 52 files
b9a0f21 verified
|
raw
history blame
No virus
3.75 kB
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.
```python
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``).
```python
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](https://bokeh.pydata.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter).
```python
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.
```python
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'``.
```python
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.
```python
sst.hvplot.heatmap(x='index.hour', y='index.month', C='temperature', cmap='reds')
```
Combining this with the information from the [section on widgets](Widgets.ipynb), we can even use the datetime features to produce a plot that steps through each month in the data.
```python
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.
```python
import xarray as xr
import hvplot.xarray # noqa
air_ds = xr.tutorial.open_dataset('air_temperature').load()
air_ds
```
Similar to how we did for sea surface temperature above, we can get the distribution of air temperature by month.
```python
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.
```python
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](https://xarray.pydata.org/en/stable/time-series.html).
### 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.
```python
sst.hvplot(label="original") * sst.hvplot(downsample=True, label="downsampled")
```