Skip to content

Commit 78d2454

Browse files
committed
no color in hovertemplate when contrast has been modified
1 parent bd25f05 commit 78d2454

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,21 +422,25 @@ def imshow(
422422
fig = go.Figure(data=trace, layout=layout)
423423
fig.update_layout(layout_patch)
424424
# Hover name, z or color
425-
if trace["type"] == "heatmap":
426-
hover_name = "z"
427-
elif img.ndim == 2:
428-
hover_name = "color[0]"
425+
if binary_string and not (img_rescaled.max() == img.max()):
426+
hovertemplate = "%s: %%{x}<br>%s: %%{y}<extra></extra>" % (
427+
labels["x"] or "x",
428+
labels["y"] or "y",
429+
)
429430
else:
430-
hover_name = "color"
431-
fig.update_traces(
432-
hovertemplate="%s: %%{x}<br>%s: %%{y}<br>%s: %%{%s}<extra></extra>"
433-
% (
431+
if trace["type"] == "heatmap":
432+
hover_name = "z"
433+
elif img.ndim == 2:
434+
hover_name = "color[0]"
435+
else:
436+
hover_name = "color"
437+
hovertemplate = "%s: %%{x}<br>%s: %%{y}<br>%s: %%{%s}<extra></extra>" % (
434438
labels["x"] or "x",
435439
labels["y"] or "y",
436440
labels["color"] or "color",
437441
hover_name,
438442
)
439-
)
443+
fig.update_traces(hovertemplate=hovertemplate)
440444
if labels["x"]:
441445
fig.update_xaxes(title_text=labels["x"])
442446
if labels["y"]:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,11 @@ def test_imshow_hovertemplate(binary_string):
285285
fig.data[0].hovertemplate
286286
== "x: %{x}<br>y: %{y}<br>color: %{color}<extra></extra>"
287287
)
288+
fig = px.imshow(img_gray, binary_string=binary_string)
289+
if binary_string:
290+
assert fig.data[0].hovertemplate == "x: %{x}<br>y: %{y}<extra></extra>"
291+
else:
292+
assert (
293+
fig.data[0].hovertemplate
294+
== "x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>"
295+
)

0 commit comments

Comments
 (0)