如何显示plotly.express直方图的悬停数据中的所有出现情况
我正在尝试在 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()
如您所见,悬停数据仅显示值从字数
和计数
。我正在尝试找到一种方法,将使用与给定垃圾箱相关的单词的发言者列表合并到其悬停数据中。我尝试将 ['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()
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您准备好数据框,则可以将其作为条形图。
If you prepare your data frame you can do this as a bar figure.