Black Lives Matter. Please consider donating to Black Girls Code today.

How to add annotations to a heatmap?

Hi!
I have a pretty standard heatmap and I cannot figure out how to add for each box the z-value as a label. Not just as hover but fixed on the heatmap. I am using plotly in what I think is a pretty standard way. I couldn’t figure it out from the doc. Here is the code:

data=[go.Heatmap(x=x_values,y=y_values,
z=z_values,colorscale=‘Jet_r’)]

layout=go.Layout(title=‘My Data’,autosize=False,
width=700, height=600,
xaxis = dict(
tickmode = ‘linear’,
dtick = 1))

fig=go.Figure(data=data,layout=layout)

Thank you for any help!

pbecker126

Hi @pbecker126,

Take a look at these tutorials: https://plotly.com/python/annotated-heatmap/

Thank you Empet! That worked. I had to switch to the Figure Factory Annotated Heatmap, and change the format of the x,y,z lists but I got it to work.

Followon question. If I want to add some divider lines (maybe they are called splines?) in my figure, to separate sections of the heatmap, is there a way to do that?

Thanks again.

@pbecker126 when fig = ff… is already defined.
update the heatmap as follow:

fig.data[0].update(xgap=1, ygap=1)