简体   繁体   English

Plotly:悬停模板:custom_data 未显示

[英]Plotly: hovertemplate : custom_data not showing

I have the following dataframe我有以下 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.出于某种原因custom_data没有从data dataframe 中提取值,或者我的脚本有一些我无法解决的错误。 Any help would be appreciated.任何帮助,将不胜感激。 Thank you in advance!先感谢您!

Your code has the wrong curly brackets in the label dictionary.您的代码在 label 字典中有错误的大括号。 Also, since it is express, you can specify df to specify the column name.此外,由于它是 express,您可以指定 df 来指定列名。 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.此外,正如您在评论中提到的,您需要 % 作为标识符,并且需要在 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} ?您可能只是在{contratante}{contratada}之前缺少一个%吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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