简体   繁体   中英

Plotly: hovertemplate : custom_data not showing

I have the following dataframe

publicationDate signingDate  nif_contratante                                        contratante  ...                                         contratada         contract_date  price reported
0      24-02-2020  04-02-2020      600081290.0             Agrupamento de Escolas de Porto de Mós  ...               The Irish College of English Limited  2020-02-04T00:00:00Z   7914      NaN
1      26-02-2020  13-02-2020      600000052.0        Instituto de Oftalmologia do Dr. Gama Pinto  ...                 Instituto de Soldadura e Qualidade  2020-02-13T00:00:00Z     90      NaN
2      27-02-2020  27-02-2020      501442600.0  Instituto do Emprego e da Formação Profissiona...  ...  ASSOCIAÇÃO HUMANITÁRIA DOS BOMBEIROS VOLUNTÁRI...  2020-02-27T00:00:00Z    500      NaN

And the following code:

data = pd.read_csv('basecovid19_ad.csv')

chart3=px.bar(x=data['contract_date'], y=data['price'], color= data['price'],
            range_x=['2020-03-01','2021-05-01'],
            labels=dict(x="Data de Publicação", y="Valor (M€)", color="Valor em Euros",
            custom_data=[data['contratante'], data['contratada']])
            )
            
chart3.update_xaxes(title_font=dict(size=18, family='Lato', color='#1a1a1e'))

chart3.update_yaxes(title_font=dict(size=18, family='Lato', color='#1a1a1e'))

chart3.update_traces(
    hovertemplate="<br>".join([
        "DATA: %{x}",
        "VALOR: %{y}",
        "Entidade: {contratante}",
        "Empresa: {contratada}",
     ])
    )
chart3.show()

This is the result:
产生的悬停标签

For some reason custom_data is not pulling the values from the data dataframe, or my script has some error that I'm not able to sort out. Any help would be appreciated. Thank you in advance!

Your code has the wrong curly brackets in the label dictionary. Also, since it is express, you can specify df to specify the column name. I am fixing the custom data. Also, as you mentioned in your comment, you need % as an identifier and you need to specify it in the index of custom_data.

import plotly.express as px

chart3=px.bar(data, x=data['contract_date'], y=data['price'], color=data['price'],
            range_x=['2020-01-01','2021-05-01'],
            labels=dict(x="Data de Publicação", y="Valor (M€)", color="Valor em Euros"),
            custom_data=['contratante', 'contratada']
            )
            
chart3.update_xaxes(title_font=dict(size=18, family='Lato', color='#1a1a1e'))

chart3.update_yaxes(title_font=dict(size=18, family='Lato', color='#1a1a1e'))

chart3.update_traces(
    hovertemplate="<br>".join([
        "DATA: %{x}",
        "VALOR: %{y}",
        "Entidade: %{customdata[0]}",
        "Empresa: %{customdata[1]}",
     ])
    )
chart3.show()

Are you perhaps just missing a % before {contratante} and {contratada} ?

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