Skip to content

Heatmap customdata needs to be 3D in Plotly>=6.0.0 #7607

Closed

Description

@celia-lm

Description of the issue

In plotly<6.0.0, this worked:

hovertemplate="n: %{customdata}" if customdata was a 2D array.

In plotly>=6.0.0, that didn’t work anymore. No error is displayed, but the value is not replaces and what is seen when the user hovers on the plot is n: %{customdata}, not, for example, n: 100.

In heatmaps, customdata now (plotly>=6.0.0) needs to be a 3D array, with the third dimension being as long as the number of custom datapoints for each point. That is, customdata needs to have dimensions [len_x, len_y, n_customdata].

In the example from the docs (link), this is done with np.dstack(tup) because two customdata values are passed for each (x,y,z) point.

However, np.dstack can’t be used directly with an existing 2D array because it creates the third dimension as the first one . So doing b=np.dstack(a) where a.shape=(5,3) results in b.shape=(1,5,3) and what the heatmap requires is is b.shape=(5,3,1).

Creating a 3D array with depth 1 from a 2D array can be done like: a[...,None].

Then, in the figure plot %{customdata[0]} will need to be specified in the hovertemplate.

Expected behaviour

Two options:

  • Raise a warning based on the dimensions of customdata (e.g. if len(customdata.shape)<3). The warning could be similar to: "customdata needs to be 3D. The current value of customdata is 2D. You can make the current value of customdata 3-dimensional by applying this method: current_value[...,None]."
  • Coerce 2D customdata values to 3D.

Code to reproduce

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
np.random.seed(0)
z1, z2, z3 = np.random.random((3, 7, 7))

customdata_two_values = np.dstack((z2, z3))
customdata_one_value_wrong = np.dstack(z2)
customdata_one_value_right = z2[...,None]

print(f"Shape of z2: {z2.shape}") # (7,7)
print(f"Shape of np.dstack((z2, z3)): {customdata_two_values.shape}") # (7, 7, 2)
print(f"Shape of np.dstack(z2): {customdata_one_value_wrong.shape}") # (1, 7, 7)
print(f"Shape of z2[...,None]: {customdata_one_value_right.shape}") # (7, 7, 1)

fig = go.Figure()
fig.add_trace(go.Heatmap(
    z=z1,
    customdata=customdata_one_value_right,
    hovertemplate='<b>z1:%{z:.3f}</b><br>z2:%{customdata[0]:.3f}',
    )
)

# code that worked in plolty<6.0.0
# fig = go.Figure()
# fig.add_trace(go.Heatmap(
#     z=z1,
#     customdata=z2,
#     hovertemplate='<b>z1:%{z:.3f}</b><br>z2:%{customdata:.3f}',
#     )
# )

fig.show()

Activity

  1. gvwilson commented on Sep 18, 2025

    @gvwilson
    Contributor

    We'll go with "raise a warning" - thank you.

  2. added
    bugsomething broken
    P1needed for current cycle
    cscustomer success
    on Sep 18, 2025
  3. emilykl commented on Oct 27, 2025

    @emilykl
    Contributor

    This is a Plotly.js issue; moving to Plotly.js repo

  4. added
    bugsomething broken
    P1needed for current cycle
    cscustomer success
    on Oct 27, 2025
  5. transferred this issue fromplotly/plotly.pyon Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1needed for current cyclebugsomething brokencscustomer success

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions