I wanted to have numeric labels for the plotly annotated heatmap using the plotly.figure_factory.create_annotated_heatmap.
Below is the code to reproduce it:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import numpy as np
fig = make_subplots(rows=n_rows, vertical_spacing=0.0,horizontal_spacing=0.0,shared_yaxes=True,shared_xaxes=True)
anno = list()
for row in range(n_rows):
annotation = test.iloc[[row]].applymap(lambda x: x if (x==test.iloc[[row]].values.max() or
x ==test.iloc[[row]].values.min()) else " ")
fig1 = ff.create_annotated_heatmap(z=np.array(test.iloc[[row]]), x = cols,
y=[test.iloc[[row]].index[0]], showscale=False,colorscale=colorscale,
annotation_text = np.array(annotation),
hovertemplate='<i>Mean</i>:%{z}'+'<extra></extra>')
fig.append_trace(fig1.data[0], row=row+1, col=1)
anno += list(fig1.layout.annotations)
for ann in anno:
if test.index.tolist().index(ann['y'])!=0:
ann['yref'] = 'y'+str((test.index.tolist().index(ann['y']))+1)
fig.add_annotation(ann)
# fig.update_xaxes(showticklabels=False, fixedrange=True)
# fig.update_yaxes(fixedrange=True)
# fig.layout.update(clickmode='select')
# config = dict({'scrollZoom': False,'dragModeBar':False})
# fig.layout.update(dragmode=False)
fig.layout.update(xaxis={'side':'top','showticklabels':True})
fig.show()
below is the test dataframe:
and below is the cols variable having all elements of type string:
and below is the resulting plot:
Does anyone know the reason for this and a fix?