pythondata-visualizationholoviewsdatashader

Timeseries with multiple colors and legend using Datashader and Holoviews


I would like to plot a timeseries dataframe with 3 columns, one for each curve. I would like each curve to have its own color and also display a legend, as hvplot() does by default.

Here is a self-contained example:

import numpy as np
import pandas as pd
import hvplot.pandas
import datetime
from holoviews.operation.datashader import datashade

n=1000
start = datetime.datetime(2010, 10, 1, 0)   # Start time
datetimerange = [start + datetime.timedelta(minutes=1)*i for i in range(n)]
A = np.random.randint(5, size=n)
B = np.random.randint(20, 40, size=n)
C = np.random.randint(10, 20, size=n)
d = {'datetime': datetimerange, 'A': A, 'B': B, 'C': C}
df = pd.DataFrame(d).set_index('datetime')

df.hvplot(cmap=['red', 'blue', 'green']) + datashade(df.hvplot(cmap=['red', 'blue', 'green']))

Here is the result (without datashader on the left, with datashader on the right):

enter image description here

When passing the plot to datashader, the colors and legend are lost. Using the datashade=True argument of hvplot has the same result.

There is a tutorial on timeseries plotting in the Datashader documentation but it's quite complicated, it uses datashader.transfer_functions.shade() as the basis to manipulate the graphs without much introduction on how this works and the API isn't much clearer. I would just like to maintain these basic plot features that bokeh/hvplot provides by default, I'm not sure the reason why datashader isn't preserving them, so I don't know what to fix.

How can I signal to datashader to preserve the different colors and plot a legend?


Solution

  • Here is half the answer, unfortunately this doesn't give a legend. Please note that .hvplot() has the argument datashade=True built in:

    df.reset_index().melt(id_vars='datetime').hvplot.line(
        x='datetime', 
        y='value', 
        by='variable', 
        datashade=True, 
        dynamic=False,
    )