如何在 plotly.express 直方图的悬停数据中显示所有事件

How to show all occurrences in the hover data of a plotly.express histogram

我正在尝试使用 hover_data 参数在 plotly 中构建一个直方图,它可以显示直方图 bin 中其他列的数据。我没有发现任何与此类似的问题,但可能我没有使用正确的白话来表达我的问题。我对社区的任何建议持开放态度。出于示例的目的,采用以下小型数据集:

import pandas as pd

word_data = {'author':['Martin Luther King Jr.',
                       'Martin Luther King Jr.',
                       'Martin Luther King Jr.',
                       'Malcolm X',
                       'Malcolm X',
                       'Fred Hampton',
                       'Fred Hampton',
                       'James Baldwin',
                       'James Baldwin'], 
             'words': ['dream', 'color', 'nonviolence',
                       'color', 'rights',
                       'panthers', 'rights',
                       'color', 'rights']}

words_df = pd.DataFrame(word_data)

Here is a screenshot of the output of the dataset for reference。请原谅数据集中潜在的历史错误——我只是用它作为例子。

I have built a plotly histogram like this,但如您所见,悬停数据仅显示 wordscount 的值。我正在尝试找到一种方法,将使用过的与给定 bin 关联的发言人列表合并到其悬停数据中。我尝试将 ['author'] 传递给 hover_data 参数,但这似乎不起作用。有谁知道实现此目标的方法?

上面的截图是我的代码:

import plotly.express as px

fig = px.histogram(words_df, x='words', hover_data=['author'],
                  labels={
                      'words': 'Most Common Words'
                  },
                   title='Most Common Words that Speakers Use'
                  ).update_xaxes(categoryorder='total descending').update_layout(yaxis_title='Number of Speakers')
fig.show()

谢谢!

如果您准备好数据框,您可以将其作为 bar 图。

import pandas as pd
import plotly.express as px

word_data = {
    "author": [
        "Martin Luther King Jr.",
        "Martin Luther King Jr.",
        "Martin Luther King Jr.",
        "Malcolm X",
        "Malcolm X",
        "Fred Hampton",
        "Fred Hampton",
        "James Baldwin",
        "James Baldwin",
    ],
    "words": [
        "dream",
        "color",
        "nonviolence",
        "color",
        "rights",
        "panthers",
        "rights",
        "color",
        "rights",
    ],
}

words_df = pd.DataFrame(word_data)

px.bar(
    words_df.groupby("words", as_index=False)
    .agg(count=("words", "size"), speakers=("author", list))
    .sort_values(["count", "words"], ascending=[0, 1]),
    x="words",
    y="count",
    hover_data=["speakers"],
    title="Most Common Words that Speakers Use",
).update_layout(xaxis_title="Most Common Words", yaxis_title="Number of Speakers")