2

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:
Resulting Hover Label

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!

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.