I have 2 dfs; the first one has values to make a heatmap and the 2nd df has metadata that I want to use on hover.
I've tried many things and this is almost working except there's some behavior I don't understand:
import plotly.express as px
import pandas as pd
import difflib
df1 = pd.DataFrame([[22,33],[23,31]], columns=['A','B'])
df2 = pd.DataFrame([['top_left','top_right'],['bottom_left','bottom_right']], columns=['A','B'])
print(df1)
print(df2)
fig = px.imshow(df1,
color_continuous_scale=px.colors.diverging.RdYlGn,
title='QC')
def ff(sample, pos, tmp):
d=tmp.to_dict()
s2 = list(d.keys())[0]
print(sample, pos, s2, s2 == sample, s2 == 'A', sample == 'A', type(sample), type(s2), list(difflib.ndiff([sample], [s2])))
return sample, pos #1, tmp.loc[pos, sample]
fig.update_traces(hovertemplate="Sample: %{x} <br> Position: %{y} <br> Score: %{z}" +\
f" <br> Depths: {ff('%{x}', '%{y}', df2)}")
fig.show()
gives
A B
0 22 33
1 23 31
A B
0 top_left top_right
1 bottom_left bottom_right
%{x} %{y} A False True False <class 'str'> <class 'str'> ['- %{x}', '+ A']
So s2 != sample because it's still %{x} (not 'A'). Which would explain why this didn't work #1, tmp.loc[pos, sample]. I have no idea how to get around this. Any help appreciated!
Just to be clear - what I'm trying to get is 'top_left' if I hover over the top left (A0) of the heatmap.