Skip to content

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

@gjkoplik

Description

@gjkoplik

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions