绘图中形状的悬停信息

时间:2020-06-05 16:44:05

标签: python hover plotly

我知道轨迹(标记/线)有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",
        )

当我将鼠标悬停在这两点上时,我将获得一个带有信息的悬停模板。如何获得形状相似的东西?

2 个答案:

答案 0 :(得分:1)

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

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

其余的代码段在形状的数量,分配给它们的颜色以及相应的迹线方面都设置得很灵活,以便在形状的右下角留下小点。

情节:

enter image description here

如果可以使用,我们可以讨论编辑hoverinfo中显示的内容的方法。

完整代码:

# 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()

答案 1 :(得分:0)

我想到了一个令我满意的解决方案。 只需绘制一个形状。您将无法看到悬停文字。但是,如果在图形的顶部添加填充填充的迹线,然后将迹线设置为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()