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')
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()