Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly express imshow hover text

I’m trying to make plotly express imshow accept two different hover text values that are different to the axis values, using the code below:

matrix = graph_to_matrix(edf, G.nodes)
x_axis = list(G.nodes)
y_axis = x_axis[::-1]

#these are what I want to add :D
custom_data = np.dstack([np.array([genome_protein_data[n]['product'] for n in x_axis]),
                                               np.array([genome_protein_data[n]['product'] for n in y_axis])])
######

figure_test = px.imshow (gmat[::-1], 
                         x =  x_axis, 
                         y = y_axis, 
                         labels=dict(x="Target", 
                                     y="Source", 
                                     color="Number"))
fig.update_layout(coloraxis_colorbar_x=0.8)
#this doesn't work - from https://stackoverflow.com/a/63185950/11357695
fig.update_traces(
    hovertemplate="<br>".join([
                    "X: %{x}",
                    "X product: %{customdata[0].3f}",
                    "Y: %{y}",
                    "Y product: %{customdata[1].3f}",
    ])
)

figure_test.write_html('my_file.html')

I get almost what I want, but my custom data isn’t substituting: enter image description here

How do I get the proper x/y product values?

Cheers! Tim

like image 896
Tim Kirkwood Avatar asked Jun 07 '26 21:06

Tim Kirkwood


1 Answers

Hi I got what I wanted eventually - key seems to be makeing arrays of the same dimension as your data, one array per hover val. Was greatly helped by r-beginners and this and thisenter link description here. Here is my code:

matrix = graph_to_matrix(edf, G.nodes)
x_axis = list(G.nodes)
y_axis = x_axis[::-1]

#x axis array - vals in a single col are the same
z1 = np.array([[genome_protein_data[x_axis[i]]['product'] for i in x_axis] for row in range(len(x_axis))])#num rows == num cols

#y axis array - vals in a single row are the same
z2 = np.array([[genome_protein_data[y_axis[i]]['product'] for i in range(len(y_axis))] for i in y_axis])

figure_test = px.imshow (gmat[::-1], 
                        x =  x_axis, 
                        y = y_axis, 
                        labels=dict(x="Target", 
                                    y="Source", 
                                    color="Number of sharedmodules"))
figure_test.update_layout(coloraxis_colorbar_x=0.8)
figure_test.update(data=[{'customdata': np.dstack((z1, z2)),
    'hovertemplate': "(x) %{x} product: %{customdata[0]}<br>(y) %{y} product: %{customdata[1]}<br>%{z}"}])
like image 194
Tim Kirkwood Avatar answered Jun 10 '26 11:06

Tim Kirkwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!