直接标签和悬停信息在情节上有所不同 [英] Direct labels and hover info to be different in plotly
问题描述
我有堆积的条形图.在条形图的细分上,我想添加数字(y值),在鼠标悬停时,我想显示一些不同的信息(而不是name或x或y值).
I have a stacked bar chart. On the segments in the bar chart I want to add the numbers (y values) and on hover I want some different information to be shown (not name or x or y values).
有可能吗?从我看到的hoverinfo
来看,我们只复制文本中的值或添加名称.
Is it possible? From what I can see hoverinfo
let's you only duplicate the values in text or add the name.
import plotly
from plotly.graph_objs import Layout, Bar
hoverinformation = ["test", "test2", "test3", "test4"] # this info I would like it on hover
data = []
for index, item in enumerate(range(1,5)):
data.append(
Bar(
x="da",
y=[item],
name="Some name", # needs to be different than the hover info
text=item,
hoverinfo="text",
textposition="auto"
)
)
layout = Layout(
barmode='stack',
)
plotly.offline.plot({
"data": data,
"layout": layout
})
这是一个简单的堆栈栏,悬停时的值将在pandas数据框中或在变量hoverinformation
This is a simple stack bar and the value on hover would be either in a pandas dataframe or in a variable hoverinformation
最好是一种不涉及在另一个之上创建2个地块的解决方案...
Preferably a solution that doesn't involve the creation of 2 plots one on top of the other...
推荐答案
是的,您可以使用hovertext
元素执行此操作. 文档
Yes you can do this by using the hovertext
element. Docs
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
hoverinformation = ["test", "test2", "test3", "test4"] # this info I would like it on hover
data = []
for index, item in enumerate(range(1,5)):
data.append(
go.Bar(
x="da",
y=[item],
hovertext=hoverinformation[index], # needs to be different than the hover info
text=[item],
hoverinfo="text",
name="example {}".format(index), # <- different than text
textposition="auto",
)
)
layout = go.Layout(
barmode='stack',
)
iplot({
"data": data,
"layout": layout
})
这篇关于直接标签和悬停信息在情节上有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!