简体   繁体   中英

Plotly express box plot hover data not working

Trying to add data to hover of boxplot express in plotly and following the instructions here in plotly 5.4.1. It is mentioned in the tutorial that additional information to be shown in the hover can be added by hover_data and hover_name argument. However, The additional hover data, in this case information from continent column, is not presented in the hover. I am not sure what is going wrong here? (Here is the code I test in Google colab)

import plotly.express as px
import pandas as pd
import numpy as np
np.random.seed(1234)

df = pd.DataFrame(np.random.randn(20, 1),columns=['Col1'])
df['country']=['canada','france']*10
df['continent']=['america','europe']*10

fig = px.box(df, x="country", y="Col1",  hover_data=['continent'])

fig.show()

Here is what i get in google colab:

plotly boxplot in google colab

  • this is similar to Change Plotly Boxplot Hover Data
  • boxplot hover info is within javascript layer of plotly . Hence have overlayed a bar plot where hover can be controlled in way you require. When you hover over boxplot you get standard boxplot hover. bar different hover info
import plotly.express as px
import pandas as pd
import numpy as np

np.random.seed(1234)

df = pd.DataFrame(np.random.randn(20, 1), columns=["Col1"])
df["country"] = ["canada", "france"] * 10
df["continent"] = ["america", "europe"] * 10

fig = px.box(df, x="country", y="Col1", hover_data=["continent"])

fig.add_traces(
    px.bar(
        df.groupby(["country", "continent"], as_index=False).agg(
            base=("Col1", "min"), y=("Col1", lambda s: s.max() - s.min())
        ),
        x="country",
        base="base",
        y="y",
        hover_data={"continent":True, "country":True, "base":False, "y":False},
    )
    .update_traces(opacity=0.1)
    .data
).update_layout(bargap=0.8)


fig

在此处输入图像描述

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