简体   繁体   中英

Hoverover template Label titles change plotly python

I am trying to change the hoverover template to show Model name, Score and scaling technique, I am able to show the first two titles but I am not able to succeed in showing the third (ie variable legend) in the tooltip Hoverover.

Below is the attached image where I am able to change the hover template of first two. I need to replace the third label Model:%{Model_Names} to be legend variable names, Could someone please help

在此处输入图像描述

Below is my code.

import plotly.express as px

fig = px.bar(compareModels_aft_Cleansing, x="Base_Models", y=["Base_Models_Scores", 
                                                     "Standard_scaled_scores", "Min_Max_scaled_scores", 
                                                     "Scaling & feature selection_scores"],
              title="Training Scores", barmode='group', text = 'value',
            hover_name="Base_Models",
            hover_data={'Base_Models':False}, # remove species from hover data                     
                            )
Model_Names = ['Base_Models_Scores', 'Standard_scaled_scores', 'Min_Max_scaled_scores']
fig.update_traces(hovertemplate = '%{label}: <br>Score:%{text} <br>Model: %{Model_Names}')
fig.show()

Please find the attached image of dataframe compareModels_aft_Cleansing below.

在此处输入图像描述

To achieve the desired hover text you can use the following:

fig.update_traces(hovertemplate = '%{label}: <br>Score:%{text} <br>Model: %{data.name}<extra></extra>')

Couple of things to note:

  1. %{data.name} is event data that omitted by the js backend, more info here .

  2. The legend names were already displayed in the secondary box, to remove that use <extra></extra> as described here .

Anything contained in tag <extra> is displayed in the secondary box, for example <extra>{fullData.name}</extra> . To hide the secondary box completely, use an empty tag <extra></extra> .

Full example:

import pandas as pd

data = {'Base_Models': ['SVM'], 
        'Base_Models_Scores': [.625], 
        'Scaling & feature selection_scores': [.75], 
        'Standard_scaled_scores': [.8], 
        'Min_Max_scaled_scores': [.95]}

compareModels_aft_Cleansing = pd.DataFrame.from_dict(data)

import plotly.express as px

fig = px.bar(compareModels_aft_Cleansing, x="Base_Models", y=["Base_Models_Scores", 
                                                     "Standard_scaled_scores", "Min_Max_scaled_scores", 
                                                     "Scaling & feature selection_scores"],
              title="Training Scores", barmode='group', text = 'value',
            hover_name="Base_Models",
            hover_data={'Base_Models':False}, # remove species from hover data                     
                            )

Model_Names = ['Base_Models_Scores', 'Standard_scaled_scores', 'Min_Max_scaled_scores']
fig.update_traces(hovertemplate = '%{label}: <br>Score:%{text} <br>Model: %{data.name}<extra></extra>')
fig.show()

在此处输入图像描述

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