Python 直接标签和悬停信息在绘图中不同
我有一个堆叠的条形图。在条形图的线段上,我想添加数字(y值),在悬停时,我想显示一些不同的信息(不是名称或x或y值) 可能吗?从我看到的Python 直接标签和悬停信息在绘图中不同,python,plotly,Python,Plotly,我有一个堆叠的条形图。在条形图的线段上,我想添加数字(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 =
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
})
这是一个简单的堆栈条,悬停时的值可能在数据框中,也可能在变量hoverinformation
最好的解决方案是不需要一个一个地创建两个绘图…是的,您可以使用
hovertext
元素来实现这一点
导入plotly.graph\u objs as go
从plotly.offline导入下载\u plotlyjs,初始化\u笔记本\u模式,绘图,iplot
初始笔记本模式(已连接=真)
hoverinformation=[“test”、“test2”、“test3”、“test4”]#我想把这些信息放在hover上
数据=[]
对于索引,枚举中的项(范围(1,5)):
data.append(
去吧,酒吧(
x=“da”,
y=[项目],
hovertext=hoverinformation[index],#需要与悬停信息不同
text=[项目],
hoverinfo=“text”,
name=“example{}”。格式(索引)#
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
})