Skip to content

Commit eeb1988

Browse files
committed
use z instead of color in hover
1 parent 0cecbd6 commit eeb1988

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/python/plotly/plotly/express/_imshow.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,13 @@ def imshow(
380380

381381
# For 2D+RGB data, use Image trace
382382
elif img.ndim == 3 and img.shape[-1] in [3, 4] or (img.ndim == 2 and binary_string):
383+
rescale_image = True # to check whether image has been modified
383384
if zmin is not None and zmax is not None:
384385
zmin, zmax = _vectorize_zvalue(zmin), _vectorize_zvalue(zmax)
385386
if binary_string:
386-
if zmin is None and zmax is None:
387+
if zmin is None and zmax is None: # no rescaling, faster
387388
img_rescaled = img
389+
rescale_image = False
388390
elif img.ndim == 2:
389391
img_rescaled = rescale_intensity(
390392
img, in_range=(zmin[0], zmax[0]), out_range=np.uint8
@@ -429,19 +431,23 @@ def imshow(
429431
fig = go.Figure(data=trace, layout=layout)
430432
fig.update_layout(layout_patch)
431433
# Hover name, z or color
432-
if binary_string and not (img_rescaled.max() == img.max()):
434+
if binary_string and rescale_image and not (img_rescaled.max() == img.max()):
435+
# we rescaled the image, hence z is not displayed in hover since it does
436+
# not correspond to img values
433437
hovertemplate = "%s: %%{x}<br>%s: %%{y}<extra></extra>" % (
434438
labels["x"] or "x",
435439
labels["y"] or "y",
436440
)
437441
else:
438442
if trace["type"] == "heatmap":
439-
hover_name = "z"
443+
hover_name = "%{z}"
440444
elif img.ndim == 2:
441-
hover_name = "color[0]"
445+
hover_name = "%{z[0]}"
446+
elif img.ndim == 3 and img.shape[-1] == 3:
447+
hover_name = "[%{z[0]}, %{z[1]}, %{z[2]}]"
442448
else:
443-
hover_name = "color"
444-
hovertemplate = "%s: %%{x}<br>%s: %%{y}<br>%s: %%{%s}<extra></extra>" % (
449+
hover_name = "%{z}"
450+
hovertemplate = "%s: %%{x}<br>%s: %%{y}<br>%s: %s<extra></extra>" % (
445451
labels["x"] or "x",
446452
labels["y"] or "y",
447453
labels["color"] or "color",

packages/python/plotly/plotly/tests/test_core/test_px/test_imshow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def test_imshow_hovertemplate(binary_string):
292292
fig = px.imshow(img_rgb, binary_string=binary_string)
293293
assert (
294294
fig.data[0].hovertemplate
295-
== "x: %{x}<br>y: %{y}<br>color: %{color}<extra></extra>"
295+
== "x: %{x}<br>y: %{y}<br>color: [%{z[0]}, %{z[1]}, %{z[2]}]<extra></extra>"
296296
)
297297
fig = px.imshow(img_gray, binary_string=binary_string)
298298
if binary_string:

0 commit comments

Comments
 (0)