【问题标题】:Direct labels and hover info to be different in plotly直接标签和悬停信息在情节上有所不同
【发布时间】:2018-07-16 17:51:36
【问题描述】:

我有一个堆积条形图。在条形图中的段上,我想添加数字(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 个地块的解决方案……

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    是的,您可以使用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
    })
    

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2018-03-09
      • 2016-03-06
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多