Python/Plotly: px bar customize hover

Question:

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!

Asked By: Keynes is not dead

||

Answers:

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

Answered By: r-beginners

You were very close, 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. 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>.

Note also that fullData.name is the name of the (hovered) trace, which, when using px.histogram(), is set automatically according to the value of color.

Answered By: EricLavault
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.