1

I am trying to use custom data in the texttemplate of a Heatmap in Plotly, but it does not seem to work. I am using Python 3.10.12 and Plotly 5.15.0. Below there is a MWE:

import plotly.express as px
import numpy

df = px.data.medals_wide(indexed=True)
fig = px.imshow(df)

customdata = numpy.zeros(df.shape)
fig.data[0].customdata = customdata
fig.data[0].hovertemplate = 'Custom data: %{customdata}' # This works fine.
fig.data[0].texttemplate = '%{customdata}' # This fails.

fig.write_html('deleteme.html',include_plotlyjs='cdn')

Produces: enter image description here

Expected: enter image description here

1
  • customdata is "not user-visible but is included in events emitted by the figure" (ie. hover, click, select, etc.), so you can use it in hovertemplate (hover event) or in Dash callbacks, but not directly as text. As mentioned by @r-beginners, what can be used for texttemplate is what you set for text. Commented Aug 1, 2023 at 10:17

1 Answer 1

2

I can't elaborate on why custom data is not supported, but the only thing that can be used for text templates is what you set for text, so set custom data for text.

import plotly.express as px
import numpy

df = px.data.medals_wide(indexed=True)
fig = px.imshow(df)

customdata = numpy.zeros(df.shape)

fig.data[0].customdata = customdata
fig.data[0].hovertemplate = 'Custom data: %{customdata}' 
fig.data[0].text = customdata
fig.data[0].texttemplate = '%{text}'

fig.write_html('deleteme.html',include_plotlyjs='cdn')
fig.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.