Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show legend for rasterized overlay #6203

Open
droumis opened this issue Apr 22, 2024 · 0 comments
Open

Show legend for rasterized overlay #6203

droumis opened this issue Apr 22, 2024 · 0 comments
Labels
TRIAGE Needs triaging

Comments

@droumis
Copy link
Member

droumis commented Apr 22, 2024

Is your feature request related to a problem? Please describe.

When rasterizing a plot, you lose the ability to show a legend. Rasterize is used when there is a lot of data, so a legend is even more important in this context to visually parse the plots.

Describe the solution you'd like

This FR is to have the code below display a legend.

import pandas as pd
import holoviews as hv
from holoviews.operation.datashader import rasterize
hv.extension('bokeh')

df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6], 'y': [1, 2, 3, 4, 5, 6], 'source': ['a', 'a', 'b', 'b', 'c', 'c']})
df['source'] = df['source'].astype('category')

curve_dict = {}
for src, data in df.groupby('source'):
    curve_dict[src] = hv.Curve(data[['x', 'y']], 'x', 'y').opts(show_legend=True)

overlay = hv.NdOverlay(curve_dict, kdims='source').opts(show_legend=True)

rasterized_overlay = rasterize(overlay, line_width=5, aggregator=ds.by('source', ds.count())).opts(show_legend=True, responsive=True, min_height=300)

rasterized_overlay

or similar with hvplot:

import pandas as pd
import hvplot.pandas
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6], 'y': [1, 2, 3, 4, 5, 6], 'source': ['a', 'a', 'b', 'b', 'c', 'c']})
df['source'] = df['source'].astype('category')
df.hvplot.line(x='x', y='y', by='source', rasterize=True, line_width=5, legend=True, colorbar=False, responsive=True, min_height=300)
image

Describe alternatives you've considered

alt option: Manual creation of a plot to overlay

  • Downside to this approach is you loose the ability to click on an entry in the legend to hide a particular line. But unless each line is rasterized independently, I suppose this is unavoidable (unless clicking an entry was to remake the whole plot).
  • There appears to be a util in holoviews categorical_legend from this PR that I can't get to work. It's supposed to create a points element that a user would overlay with the rasterized plot to show the legend. I don't see this documented or used anywhere.
  • The census example shows an example of manually creating such an overlay.
image

alt option: rasterize one curve at a time and then overlay

  • It looks like one person on discourse figured out a solution to this by rasterizing individual curves and then overlaying
image

Additional context

@droumis droumis added the TRIAGE Needs triaging label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TRIAGE Needs triaging
Projects
None yet
Development

No branches or pull requests

1 participant