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

Cannot use Dataset to hv.Polygons with grouping, custom colors, and legend #6379

Open
1 task
Lnk2past opened this issue Sep 19, 2024 · 1 comment
Open
1 task
Labels
type: bug Something isn't correct or isn't working

Comments

@Lnk2past
Copy link

ALL software version info

(this library, plus any other relevant software, e.g. bokeh, python, notebook, OS, browser, etc should be added within the dropdown below.)

Software Version Info
  python                            3.11.9
  bokeh                             3.3.4
  holoviews                         1.19.1
  notebook                          7.2.2
  
  Debian Bullseye
  Chrome Version 128.0.6613.139 (Official Build) (64-bit)

Description of expected behavior and the observed behavior

Opening this in response to https://discourse.holoviz.org/t/dataset-to-polygons-with-groupby-legend/8183. It is not clear to me if this is all one bug, or a collection of bugs due to this happening in different ways with different setups.

When attempting to build an overlay of polygons from a dataset using a groupby, coloring
the polygons by some category, and showing the legend, I am met with:

ValueError: Only one of legend, legend_label, legend_field or legend_group may be provided, got: legend_label or legend_field

I would expect to see an overlay of polygons, colored by their category, with the legend showing the category colors. If I remove the categorical coloring, or the legend, the polygons are overlayed correctly (but I would be missing either the coloring or the legend).

If I pad my data to "close" the polygons and use hv.Path instead, the correct plot and coloring is done and a legend is provided, but the legend is incorrect (missing + incorrect entries).

If I manually construct the overlay using a loop to produce the individual hv.Polygon elements while also using a colormap, the correct overlay is produced, but the legend is incorrect (missing + incorrect entries).

If I manually construct the overlay using a loop to produce the individual hv.Path elements, then everything works (but this is way clunkier).

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

# code goes here between backticks
import holoviews as hv
import pandas as pd

hv.extension('bokeh')

df = pd.DataFrame({
    'x': [0, 1, 1, 1, 2, 2, 2, 3, 3],
    'y': [0, 0, 1, 0, 0, 1, 0, 0, 1],
    'id':['foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz'],
    'type':['a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'a']
})
hv.Dataset(df, kdims=['x', 'y']).to(hv.Polygons, groupby='id').overlay().opts(
    hv.opts.Polygons(color='type', cmap={'a':'red', 'b':'blue'}, show_legend=True)
)

# hv.ipython.show_traceback() # if in notebook

Stack traceback and/or browser JavaScript console output

Traceback (most recent call last):

  File "/root/micromamba/envs/starling/lib/python3.11/site-packages/holoviews/plotting/bokeh/element.py", line 2047, in _init_glyphs
    renderer, glyph = self._init_glyph(plot, mapping, properties)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/root/micromamba/envs/starling/lib/python3.11/site-packages/holoviews/plotting/bokeh/path.py", line 294, in _init_glyph
    renderer = getattr(plot, plot_method)(**data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/root/micromamba/envs/starling/lib/python3.11/site-packages/bokeh/plotting/_decorators.py", line 87, in wrapped
    return create_renderer(glyphclass, self.plot, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/root/micromamba/envs/starling/lib/python3.11/site-packages/bokeh/plotting/_renderer.py", line 81, in create_renderer
    legend_kwarg = pop_legend_kwarg(kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^

  File "/root/micromamba/envs/starling/lib/python3.11/site-packages/bokeh/plotting/_legends.py", line 50, in pop_legend_kwarg
    raise ValueError(f"Only one of {nice_join(LEGEND_ARGS)} may be provided, got: {nice_join(result.keys())}")

ValueError: Only one of legend, legend_label, legend_field or legend_group may be provided, got: legend_label or legend_field

Screenshots or screencasts of the bug in action

Errors and Attempted Workarounds
Error

image

hv.Path with Incorrect Legend

image

Manual Overlay of hv.Polygon with incorrect legend

image

Manual (and Correct!) Overlay with hv.Path

image

Note that I am manually setting labels and colors; trying to use a column to set values results in issues.

  • I may be interested in making a pull request to address this
@philippjfr
Copy link
Member

This issue has been mentioned on HoloViz Discourse. There might be relevant details there:

https://discourse.holoviz.org/t/dataset-to-polygons-with-groupby-legend/8183/4

@hoxbro hoxbro added the type: bug Something isn't correct or isn't working label Sep 20, 2024
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

3 participants