0

Having this dataframe:

df_grafico2 = pd.DataFrame(data = {
    "Usos" : ['Total','BK','BI','CyL','PyA','BC','VA','Resto','Total','BK','BI','CyL','PyA','BC','VA','Resto'],
    "Periodo" : ['Octubre 2021*','Octubre 2021*','Octubre 2021*','Octubre 2021*','Octubre 2021*','Octubre 2021*','Octubre 2021*','Octubre 2021*','Octubre 2022*','Octubre 2022*','Octubre 2022*','Octubre 2022*','Octubre 2022*','Octubre 2022*','Octubre 2022*','Octubre 2022*'],
    "Dolares" : [5247,869,2227,393,991,606,104,57,6074,996,2334,601,1231,676,202,33]
    })

I've tried this plot:

plot_impo_usos = px.histogram(df_grafico2[df_grafico2.Usos != "Total"], x = "Usos", y = "Dolares",color="Periodo", barmode="group", template="none",
                              hover_data =["Periodo", "Dolares"],
                              )
plot_impo_usos.update_yaxes(tickformat = ",",title_text='En millones de USD')
plot_impo_usos.update_layout(separators=",.",font_family='georgia', title_text = "Importación por usos económicos. Octubre de 2022 y octubre de 2021",
                  legend=dict(
       yanchor="top", orientation = "h",
       y=1.07,
       xanchor="left",
       x=0.3))

But the hover changes automatically into "sum of Dolares", and it won't be possible to get the "Dolares" name back, even if I try this:

labels={"Usos":"Uso","sum of Dólares": "Dólares"}

hover sum of dol

The best outcome would be a hover template with: "Periodo", "Uso" and "Dolares" (with $ before).

I've tried this, but it won't work neither:

plot_impo_usos.update_traces(hovertemplate='Periodo: %{color} <br>Uso: %{x} <br>Dolares: $%{y}')

enter image description here

Help is much appreciated!

2 Answers 2

2

The easiest way to do hover text is to use fig.data (in your case, plot_impo_usos.data). to get the graph configuration data, so it is easy to customize it. So copy the hover template that is set up for the two listograms and edit it. Being able to customize it with the configuration information gives you more freedom of expression.

import plotly.express as px

plot_impo_usos = px.histogram(df_grafico2[df_grafico2.Usos != "Total"],
                              x = "Usos",
                              y = "Dolares",
                              color="Periodo",
                              barmode="group",
                              template="none",
                              hover_data =["Periodo", "Dolares"],
                              )

plot_impo_usos.data[0].hovertemplate = 'Periodo: Octubre 2021*<br>Usos: %{x}<br>Dolares: $%{y}<extra></extra>'
plot_impo_usos.data[1].hovertemplate = 'Periodo: Octubre 2022*<br>Usos: %{x}<br>Dolares: $%{y}<extra></extra>'

plot_impo_usos.update_yaxes(tickformat = ",",
                            title_text='En millones de USD')
plot_impo_usos.update_layout(separators=",.",
                             font_family='georgia',
                             title_text = "Importación por usos económicos. Octubre de 2022 y octubre de 2021",
                             legend=dict(
                                 yanchor="top",
                                 orientation = "h",
                                 y=1.07,
                                 xanchor="left",
                                 x=0.3
                             )
                            )

plot_impo_usos.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

In hovertemplate, you just need to use %{fullData.name} instead of %{color} :

plot_impo_usos.update_traces(
    hovertemplate='Periodo: %{fullData.name}<br>Uso: %{x}<br>Dolares: %{y:$,.2f}<extra></extra>'
)

NB. fullData.name refers to the name of the (hovered) trace, which, when using plotly express functions like px.histogram(), is set automatically according to the value of color (as it creates one trace per color).

Also, when using hovertemplate, there is this "secondary box" that appears next to the hover box, which is (quite often) annoying :

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

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.