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 add vdims to hv.Contours without setting color to vdim #6469

Open
1 task done
gjkoplik opened this issue Dec 8, 2024 · 2 comments
Open
1 task done

Cannot add vdims to hv.Contours without setting color to vdim #6469

gjkoplik opened this issue Dec 8, 2024 · 2 comments
Milestone

Comments

@gjkoplik
Copy link

gjkoplik commented Dec 8, 2024

ALL software version info

Software Version Info
Python: 3.12.7 (`uv` venv)
Holoviews: 1.19.1
Bokeh: 3.6.0
Ubuntu 20.04.5 LTS
Browser: Chrome

Description of expected behavior and the observed behavior

I'm trying to plot multiple lines using holoviews with the bokeh back end showing additional hover information. I've settled on using hv.Contours for my use case***.

To include the hover info, I added it to the vdims parameter. I expected this to result in showing my hover information, but if I specified for example color="black" as well, then the lines would still be black. (I would also expect the lines to be a single color by default for my use case, but I believe the primary use of hv.Contours is contours on an image, which probably should be colored by default, so that's fine.)

In practice, although this sets my hover info as expected, it also sets that vdim as the color of the lines, even if I specify a non-vdim line color (see example below).

(Note: if I color the lines by an additional / different vdim, then the lines get colored as expected, which at least presents a natural hack solution on my end.)

***I did not include it in this minimal example, but I need to support sometimes including line-specific viz kwargs on each line, for example line width, alpha, and / or color, which brought me to using hv.Contours instead of hv.Curve or hv.Path, both of which I tried before settling on hv.Contours. My lines are also composed of a potentially varying number of points (and always >2 points), which ruled out the use of hv.Segments.

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

import holoviews as hv
import numpy as np
from bokeh.models import HoverTool

hv.extension("bokeh")

# generate some random curves
num_pts = 10
rng = np.random.default_rng(0)
x = rng.random(size=num_pts * 3).reshape(-1, 3)
y = rng.random(size=num_pts * 3).reshape(-1, 3)

# generate some corresponding hover information
hover_info_variable_name = "hover_info"
hover_info = [f"Unique Hover Info: {i:.2f}" for i in rng.random(size=num_pts)]

data_dicts = [
    {"x": i, "y": j, hover_info_variable_name: k} for i, j, k in zip(x, y, hover_info)
]

# custom tooltip for showing hover info
tooltips = f"""
<div>
    <b>Custom Hover Info: @{{{hover_info_variable_name}}}</b>
</div>
"""
hover_tool = HoverTool(
    tooltips=tooltips,
    description="My Custom Hover Tool",
)

# desired behavior: black lines with correct hover info
# observed behavior: correct hover info, but lines colored by vdim
curves_with_hover_info = hv.Contours(
    data_dicts,
    kdims=["x", "y"],
    vdims=[hover_info_variable_name],  # include for hover info, NOT for color
).opts(
    show_legend=False,
    color="black",  # this gets ignored
    tools=[hover_tool],
)

# this case meets expected behavior: black lines but no hover info to correctly reference
curves_without_hover_info = hv.Contours(
    data_dicts,
    kdims=["x", "y"],  # skipping vdims here
).opts(
    show_legend=False,
    color="black",  # this gets used now
    tools=[hover_tool],
)

fig = curves_with_hover_info.opts(
    title="Hover info is correct\nbut colored by vdim"
) + curves_without_hover_info.opts(title="Color correct\nbut '???' hover info")

fig

# also ran as a script locally to confirm the issue wasn't from running in a jupyter notebook
# hv.save(fig, "./contours_bug.html")

Stack traceback and/or browser JavaScript console output

N/A

Screenshots or screencasts of the bug in action

Image

Image

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

hoxbro commented Dec 9, 2024

This is because we go down a bad route because of the legacy color_index. Setting color_index to 5 in opts will give the desired behavior but raise a warning.

else:
cidx = self.color_index+2 if isinstance(self.color_index, int) else self.color_index
cdim = element.get_dimension(cidx)

I will remove color_index in the next minor release (1.21) as part of #6445, which will fix this issue.

@hoxbro hoxbro added this to the 1.21.0 milestone Dec 9, 2024
@gjkoplik
Copy link
Author

Thanks for the quick response! I'll keep an eye out for that new release and try playing with color_index=5 in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants