我有一个堆积条形图。在条形图中的段我想添加数字(y值),并且在悬停时我想要显示一些不同的信息(不是名称或x或y值)。
有可能吗?从我所看到的hoverinfo
开始,我们只复制文本中的值或添加名称。
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
优选地,不涉及在另一个之上创建2个图的解决方案......
答案 0 :(得分:2)
是的,您可以使用hovertext
元素来完成此操作。 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
})