holoviz / holoviews

With Holoviews, your data visualizes itself.
https://holoviews.org
BSD 3-Clause "New" or "Revised" License
2.7k stars 402 forks source link

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

Open BMM3 opened 2 years ago

BMM3 commented 2 years ago

Packages:

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

hoxbro commented 2 years ago

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.