简体   繁体   中英

How do I add the p value on the hover of a plotly express heatmap?

With SciPy I get the Spearman correlation and p-values.

在此处输入图像描述

With px.imshow from plotly express I get the following plot:

热图图

At this moment the hover shows: Feature_1 , Feature_2 and their respective Correlation .

How do I make the hover also show the p_value ?

If you look at the documentation of plotly.figure_factory.create_annotated_heatmap , you will not find any attribute to change the hover data. Thus, I suggest using plotly.graph_objects as follows:

Code

import numpy as np
import plotly.graph_objects as go

z = np.round(np.random.rand(5,5),2)
p = np.round(np.random.rand(5,5),2)
    
feature_1 =  ['Buttinia1 type',
      'Buttinia2 type',
      'Buttinia3 type',
      'Buttinia4 type',
      'Buttinia5 type']

feature_2 = ['Diporopollis aff. assamica',
     'Esporas, hifas y/o cuerpos fructiferos',
     'Forro de foraminífero',
     'Sphaeromorpho',
     'Scolecodonte']

hovertext = np.empty(z.shape, dtype=object)

for i, ftr2 in enumerate(feature_2):
    for j,ftr1 in enumerate(feature_1):
        hovertext[i,j] = f'feature 1: {ftr1}<br />feature 2: {ftr2}<br />Correlation: {z[i,j]}<br />p-value: {p[i,j]}'      

fig= go.Figure(go.Heatmap(z=z,
                          x=feature_1,
                          y=feature_2,
                          hoverinfo='text',
                          hovertext=hovertext,
                          texttemplate="%{z}"))
fig.show()

Output

在此处输入图像描述

You can define features 1 nd 2 by using df.feature1.unique().tolist()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM