TODAY learn how to bring Drag & Drop to Dash with Dashboard Engine. 🌠 Sign up for the live event!

Bug: Can't add X Y labels to Figure Factory annotated heatmap

The steps to recreate the bug are simple

z = [[(i+j) for j in range(21)] for i in range(21)]
x = ['a' for i in range(len(z[0]))]

fig=ff.create_annotated_heatmap(z, x=x, y=x)
fig.show()

As you can see labels are not added and the figure is unreadable.

Hi @Mirk0_98 ,

A solution to add ticklabels is this one:

import plotly.figure_factory as ff
import numpy as np

z = [[i+j for j in range(21)] for i in range(21)]
tickvals= np.arange(len(z[0]))
x, y = np.meshgrid(tickvals, tickvals)
customdata = np.dstack((x,y))

fig=ff.create_annotated_heatmap(z)
fig.update_traces(customdata=customdata, 
                  hovertemplate="x: %{customdata[0]}<br>"+\
                                "y: %{customdata[1]}<br>z: %{z}")
fig.update_xaxes(tickvals=tickvals, ticktext=["a"]*21, showticklabels=True)
fig.update_yaxes(tickvals=tickvals, ticktext=["a"]*21, showticklabels=True)
fig.update_layout(width=700, height=700)
fig.show()

If you like to have flipped annotations (i.e. to reverse the row order), then replace the
line:

fig.update_yaxes(tickvals=tickvals, ticktext=["a"]*21, showticklabels=True)

with:

fig.update_yaxes(tickvals=tickvals, ticktext=["a"]*21, showticklabels=True, autorange="reversed")

By default, the ff.create_annotated_heatmap() does not display heatmap ticklabels. That’s why we performed the update showticklabels=True.

The values in the list /arraytickvals give positions of ticklabels, and those in ticktext, the corresponding labels. I suspect that you want different ticklabels, not just “a”, like in your minimal example.

customdata and hovertemplate is needed to display the right information on hover. If they are not provided, then on hover you’ll see: x=‘a’, y=‘a’, z=j for j in your example range.