holoviews icon indicating copy to clipboard operation
holoviews copied to clipboard

Legend is missing for hv.Scatter when using hv.dim().categorize()

Open BMM3 opened this issue 1 year ago • 1 comments

Packages:

  • Python 3.9.7
  • holoviews 1.15.0
  • jupyterlab 3.4.4
  • bokeh 2.4.3
  • datashader 0.14.1

Description: Categorizing data is very easy with the hv.dim().categorize() function. However, it has the disadvantage that it doesn't create a legend as you would normally expect it to be. I tried this for the hv.Scatter, but I assume it's similar for other plot types as well. In the example below, the first plot shows a categorization using the cmap option and this creates a legend. The second plot uses the hv.dim().categorize() function and this doesn't create a legend. The cmap would be a good workaround, but I would also like to categorize using the marker shapes as shown in plot 3. Since there is no mapping option for the marker shapes I use the hv.dim().categorize() function. But it would be great to have a legend added.

Example:

import pandas as pd
import holoviews as hv
df = pd.DataFrame({'x':[1,2,3,1,2,3], 'y': [2,4,9,3,5,10], 'DUT':[1,1,1,2,2,2]})

p1 = hv.Scatter(df, 'x', ['y', 'DUT'])
p2 = hv.Scatter(df, 'x', ['y', 'DUT'])
p3 = hv.Scatter(df, 'x', ['y', 'DUT'])

(p1.opts(color='DUT', cmap={1:'blue', 2: 'red'}, legend_position='bottom_right', size=8) + 
 p2.opts(color=hv.dim('DUT').categorize(categories={1:'blue', 2: 'red'}, default='green'), legend_position='bottom_right', size=8) + 
 p3.opts(marker=hv.dim('DUT').categorize(categories={1:'circle', 2: 'cross'}, default='square'), legend_position='bottom_right', size=8)

image

BMM3 avatar Aug 18 '22 10:08 BMM3

Thank you for the suggestion. As you have noted, we do not have the logic to infer the legend from the dimension expression.

I will mark this as a feature request.

hoxbro avatar Aug 22 '22 15:08 hoxbro