python 如何显示www.example.com直方图悬停数据中的所有匹配项plotly.express

xqk2d5yq  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(133)

我尝试在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()

正如你所看到的,悬停数据只显示了wordscount的值。我试图找到一种方法,将使用与给定bin相关的单词的发言者列表合并到悬停数据中。我尝试将['author']传递到hover_data参数中,但似乎不起作用。有人知道实现这一点的方法吗?

56lgkhnf

56lgkhnf1#

如果你准备好了数据框,你可以把它做成一个图。

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

相关问题