5

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?

gaborous
  • 15,832
  • 10
  • 83
  • 102
  • 1
    maybe you can also ask the question on https://discourse.holoviz.org/ This is where the developers of the package also answer questions. If they have a better answer, can you also post it here? I'm also curious :) – Sander van den Oord Nov 18 '20 at 20:49

1 Answers1

1

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,
)
Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
  • This hvplot solution does not coloring the groups differently. Is that a bug or a wrong usage of hvplot arguments? – fmfreeze Feb 16 '21 at 09:56
  • 1
    @fmfreeze: i wrote that I gave half the answer, so I didn't know how to solve that. But a better answer to this question can be found here: https://discourse.holoviz.org/t/how-to-customize-histogram-for-linked-large-time-series-curve-plots-with-full-code-example-for-newbies-like-me/1581 – Sander van den Oord Feb 16 '21 at 11:05
  • @SandervandenOord Thank you very very much! I accepted your answer as your comment links to the full answer, could you please add it as an addendum to your answer please? – gaborous Sep 18 '21 at 12:54