No legend with Datashader/Curves

How can I make a legend appear in the following code?

import holoviews as hv
import datashader as ds
from holoviews.operation.datashader import datashade
import panel as pn
import numpy as np
import pandas as pd

hv.extension('bokeh')

# Alternative random dataframe creation
df = pd.DataFrame(np.random.randint(0, 100, size=(50000, 2)), columns=['YZF12500', 'YZF12525'])

# Create curves for each parameter
c = hv.Curve((df.index, df['YZF12500']), kdims=['Time'], vdims=['Value'])
c2 = hv.Curve((df.index, df['YZF12525']), kdims=['Time'], vdims=['Value'])

line_overlay = hv.NdOverlay({'YZF12500': c, 'YZF12525': c2}, kdims='k')
pn.serve(datashade(line_overlay, pixel_ratio=2, line_width=4, aggregator=ds.by('k', ds.count())).opts(width=800))

Hi @CrashLandonB,

Yes my understanding is that you have to create some fake data and overlay because datashader doesn’t deal with this part itself… Something like this will get you a legend however adapted from holoviews page

import holoviews as hv
import datashader as ds
from holoviews.operation.datashader import datashade, rasterize
import panel as pn
import numpy as np
import pandas as pd

import hvplot.pandas

hv.extension('bokeh')

# Alternative random dataframe creation
df = pd.DataFrame(np.random.randint(0, 100, size=(50000, 2)), columns=['YZF12500', 'YZF12525'])

# Create curves for each parameter
c = hv.Curve((df.index, df['YZF12500']), kdims=['Time'], vdims=['Value']).opts(line_color='red')
c2 = hv.Curve((df.index, df['YZF12525']), kdims=['Time'], vdims=['Value']).opts(line_color='darkred')

#Adapted From: https://holoviews.org/user_guide/Large_Data.html
from datashader.colors import Sets1to3 # default datashade() and shade() color cycle
#color_key = list(enumerate(Sets1to3[0:num_ks]))
color_key = [('YZF12500', '#e41a1c'), ('YZF12525', '#377eb8')]
color_points = hv.NdOverlay({k: hv.Points([(0,0)], label=str(k)).opts(color=v, size=0) for k, v in color_key})

line_overlay = hv.NdOverlay({'YZF12500': c, 'YZF12525': c2}, kdims='k')
pn.serve((datashade(line_overlay, pixel_ratio=2, line_width=4, aggregator=ds.by('k', ds.count())).opts(width=800))*color_points)

I think something might be in the works to help with legends but for now to have this kind of support you need to utilise the plotting libraries features see FAQ — Datashader v0.14.2 so rasterize in hvplot / holoviews might be the direction you want

Hope it helps,

Thanks, Carl.