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

BoxWhisker plot duplicate legend entries #6486

Open
1 task
freya-who opened this issue Jan 9, 2025 · 1 comment
Open
1 task

BoxWhisker plot duplicate legend entries #6486

freya-who opened this issue Jan 9, 2025 · 1 comment
Labels
type: bug Something isn't correct or isn't working

Comments

@freya-who
Copy link

ALL software version info

Software Version Info
python 3.12.8
holoviews 1.20.0
bokeh 3.6.2

Description of expected behavior and the observed behavior

I'm creating a BoxWhisker plot using holoviews and adding a legend to it.
Expected behaviour: Legend shows the categories only once.
Observed behaviour: Legend shows the categories twice.

Complete, minimal, self-contained example code that reproduces the issue

import holoviews as hv
import numpy as np
import pandas as pd

hv.extension('bokeh')

np.random.seed(42)  
values = np.random.uniform(10, 20, size=100)
names = np.random.choice(['Name_A', 'Name_B', 'Name_C'], size=100, replace=True)

df = pd.DataFrame({'value': values, 'name': names})

hv.BoxWhisker(df, kdims='name', vdims='value').opts(
    box_color='name', cmap='Set1', show_legend=True)

Screenshots or screencasts of the bug in action

Image

  • I may be interested in making a pull request to address this

I don't know enough about the code to make a pull request, but I have done a dive in and narrowed the problem down a bit so that someone that knows their way around the code can probably easily fix this:

The problem lies somewhere between holoviews\plotting\bokeh\stats.py and holoviews\plotting\bokeh\element.py

In stats.py, two datasets and two mappings are introduced, one for the part of the boxplot above the average line and one for below it:

mapping = {
            bar_glyph+'_1': vbar_map, bar_glyph+'_2': vbar2_map, 'segment_1': seg_map,
            'segment_2': seg_map, 'segment_3': seg_map, 'segment_4': seg_map,
            'circle_1': out_map
        }

then, only for vbar_map (not for vbar2_map), the legend field is set:

if self.show_legend:
            vbar_map['legend_field'] = cdim.name

however, for some reason, in the _apply_transforms of ElementPlot in element.py, the 'legend_field' is also set for the vbar2_map, which causes it to also be rendered as a legend.

Would be great if someone found a fix for that!

@freya-who
Copy link
Author

I should maybe mention that I found a "dirty" solution by checking for key == "vbar_2" in the _init_glyphs for loop and deleted the 'legend_field' entry, so I just added

if key == "vbar_2":
    del group_style['legend_field']

after the _apply_transforms call, but that is probably not a good way to do it...

@hoxbro hoxbro added the type: bug Something isn't correct or isn't working label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

No branches or pull requests

2 participants