直接标签和悬停信息在情节上有所不同

发布于 2021-01-29 15:03:30

我有堆积的条形图。我想在条形图中的段上添加数字(y值),而在悬停时希望显示一些其他信息(而不是name或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
被浏览
103
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    是的,您可以使用hovertext元素来执行此操作。文件

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

    在此处输入图片说明



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看