如何显示plotly.express直方图的悬停数据中的所有出现情况

发布于 2025-01-14 01:19:16 字数 1970 浏览 2 评论 0原文

我正在尝试在 plotly 中构建一个直方图,它可以使用 hover_data 参数显示直方图箱中其他列的数据。作为示例,采用以下小数据集:

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)
print(words_df)

其结果(仅供参考):

                   author        words
0  Martin Luther King Jr.        dream
1  Martin Luther King Jr.        color
2  Martin Luther King Jr.  nonviolence
3               Malcolm X        color
4               Malcolm X       rights
5            Fred Hampton     panthers
6            Fred Hampton       rights
7           James Baldwin        color
8           James Baldwin       rights

我构建了以下 plotly 直方图:

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()

plotly histogram

如您所见,悬停数据仅显示值从字数计数。我正在尝试找到一种方法,将使用与给定垃圾箱相关的单词的发言者列表合并到其悬停数据中。我尝试将 ['author'] 传递到 hover_data 参数中,但这似乎不起作用。有谁知道如何实现这一目标?

I'm trying to construct a histogram in plotly that can show data from other columns in the histogram's bins using the hover_data argument. As an example, take the following small dataset:

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)
print(words_df)

Which (for reference) results in:

                   author        words
0  Martin Luther King Jr.        dream
1  Martin Luther King Jr.        color
2  Martin Luther King Jr.  nonviolence
3               Malcolm X        color
4               Malcolm X       rights
5            Fred Hampton     panthers
6            Fred Hampton       rights
7           James Baldwin        color
8           James Baldwin       rights

I've built the following plotly histogram:

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()

plotly histogram

As you can see the hover data only shows values from words and count. I am trying to find a way to also incorporate a list of the speakers who used the word associated with a given bin into its hover data. I tried passing ['author'] into the hover_data argument, but that doesn't seem to work. Does anyone know of a way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2025-01-21 01:19:16

如果您准备好数据框,则可以将其作为条形图

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")

输入图片此处描述

If you prepare your data frame you can do this as a bar figure.

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")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文