【问题标题】:Hoverinformation for shapes in plotly绘图中形状的悬停信息
【发布时间】:2020-09-24 22:51:47
【问题描述】:

我知道有用于跟踪(标记/线)的 hovertemplate/hover_text/ 选项,但我找不到形状这样的东西。 有没有办法在形状上移动时弹出悬停文本?也许是一种解决方法?

例子:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[1.5, 3],
    y=[2.5, 2.5],
    text=["Rectangle reference to the plot",
          "Rectangle reference to the axes"],
    mode="markers",
))
fig.add_shape(
        # Rectangle reference to the plot
            type="rect",
            xref="paper",
            yref="paper",
            x0=0.25,
            y0=0,
            x1=0.5,
            y1=0.5,
            line=dict(
                color="LightSeaGreen",
                width=3,
            ),
            fillcolor="PaleTurquoise",
        )

当我将鼠标悬停在这两点上时,我会得到一个带有信息的悬停模板。我怎样才能得到类似的形状?

【问题讨论】:

    标签: python hover plotly


    【解决方案1】:

    似乎无法将 hoverinfo 添加到形状中直接。但是您可以通过形状和轨迹的正确组合获得非常接近预期效果的东西。下图是通过在列表中指定两个矩形制成的:

    shapes = [[2,6,2,6],
              [4,7,4,7]]
    

    其余的代码 sn-p 设置为灵活地设置形状的数量、分配给它们的颜色以及相应的痕迹,以在形状的右下角制作那个小点。

    剧情:

    如果这是您可以使用的东西,我们可以讨论编辑悬停信息中显示的内容的方法。

    完整代码:

    # Imports
    import pandas as pd
    #import matplotlib.pyplot as plt
    import numpy as np
    import plotly.graph_objects as go
    import plotly.express as px
    
    # shape definitions
    shapes = [[2,6,2,6],
              [4,7,4,7]]
    
    # color management
    # define colors as a list 
    colors = px.colors.qualitative.Plotly
    
    # convert plotly hex colors to rgba to enable transparency adjustments
    def hex_rgba(hex, transparency):
        col_hex = hex.lstrip('#')
        col_rgb = list(int(col_hex[i:i+2], 16) for i in (0, 2, 4))
        col_rgb.extend([transparency])
        areacol = tuple(col_rgb)
        return areacol
    
    rgba = [hex_rgba(c, transparency=0.4) for c in colors]
    colCycle = ['rgba'+str(elem) for elem in rgba]
    
    # plotly setup
    fig = go.Figure()
    
    # shapes
    for i, s in enumerate(shapes):
        fig.add_shape(dict(type="rect",
                            x0=s[0],
                            y0=s[2],
                            x1=s[1],
                            y1=s[3],
                            layer='above',
                            fillcolor=colCycle[i],
                            line=dict(
                                color=colors[i],
                                width=3)))
    
    # traces as dots in the lower right corner for each shape
    for i, s in enumerate(shapes):
        fig.add_trace(go.Scatter(x=[s[1]], y=[s[2]], name = "Hoverinfo " +str(i + 1),
                                 showlegend=False,
                                 mode='markers', marker=dict(color = colors[i], size=12)))
    
        # edit layout
    fig.update_layout(yaxis=dict(range=[0,8], showgrid=True),
                      xaxis=dict(range=[0,8], showgrid=True))
    fig.show()
    

    【讨论】:

    • 感谢您的回答!昨天我有一个新想法,我认为这是解决问题的好方法: import plotly.graph_objects as go x=1 fig = go.Figure(go.Scatter(x=[0,1,2,0], y= [0,2,0,0], fill="toself", name="Example text {}".format(x), opacity=0)) fig.show() 这样我仍然可以看到悬停文本并且可以将痕迹放在实际形状的顶部时包括变量。完美的!谢谢你的时间:)
    • @DanaMiles 好的。您会考虑将完整的解决方案作为个人答案分享吗?
    • @DanaMiles 到现在为止,您已经获得了足够的声誉积分来奖励您认为有帮助或有价值的帖子:-)
    • 我对 SO 还很陌生,所以感谢您给我提示并向我展示了绳索。我添加了一个我满意的解决方案的评论。谢谢!
    【解决方案2】:

    我想到了一个我满意的解决方案。 简单地画一个形状。您将无法看到悬停文本。但是,如果您在形状顶部添加带有填充的轨迹,然后将轨迹设置为 opacity=0,您将看到轨迹中的悬停文本在移动到形状上时弹出。 再次感谢您的回复!

    import plotly.graph_objects as go    
    
    # Draw shape (you won't be able to add a hover text for it)
    fig = go.Figure()
        fig.add_shape(
            type="rect",
            x0=0, y0=0,
            x1=4, y1=3,
            fillcolor='LightSkyBlue',
            line_color='Blue',
            name='Shape 1'
        )
    
        # Adding a trace with a fill, setting opacity to 0
        fig.add_trace(
            go.Scatter(
                x=[0,0,4,4,0], 
                y=[0,3,3,0,0], 
                fill="toself",
                mode='lines',
                name='',
                text='Custom text on top of shape',
                opacity=0
            )
        )
        fig.show()
    

    【讨论】:

    • 正如我在回答中解释的那样,您所做的工作适用于参考轴而不是图形 (paper) 的形状。如果您还没有... xref="paper", yref="paper"...,那么这是可能的。您所做的就是我的答案中的链接所暗示的。
    • 跟踪会影响坐标轴边界!
    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 2016-12-21
    • 2021-10-26
    • 2023-04-06
    • 2018-02-13
    • 1970-01-01
    • 2011-05-25
    • 2015-05-12
    相关资源
    最近更新 更多