【问题标题】:Python Plotly hoverover information for two or more heatmaps两个或多个热图的 Python Plotly 悬停信息
【发布时间】:2021-10-13 06:10:36
【问题描述】:

我正在绘制 3 个热图,并且希望在我将鼠标悬停在 (x,y) 点上时显示所有 3 个热图的 z 值。

我已经看到,对于散点图,您可以使用统一的 x 来显示悬停时所有图的信息。有没有类似的方法来为热图绘制统一的 z 值?

我还看到您可以创建自定义文本的数据框并将其用作悬停标签,但这对于我正在尝试做的事情来说似乎有点过分了。

谢谢

【问题讨论】:

    标签: python hover plotly label heatmap


    【解决方案1】:
    import pandas as pd
    import numpy as np
    import plotly.graph_objects as go
    
    dfs = [pd.DataFrame(index=list("abcd"), columns=list("ab"),
                        data=np.where(np.random.randint(1, 8, [4, 2]) == 1,
                                      np.nan, np.random.randint(1, 500, [4, 2]),)
                       )
           for i in range(3)]
    
    # create text array same shape as z
    text = pd.concat(dfs).groupby(level=0).agg({c:lambda v: ", ".join(v.astype(str)) for c in dfs[0].columns}).values
    
    # figure
    go.Figure([go.Heatmap(z=df.values, x=df.columns, y=df.index, name=i, text=text, hoverinfo="text")
        for i, df in enumerate(dfs)
    ])
    
    

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 2016-12-21
      • 2016-07-12
      • 2018-01-29
      • 2021-11-28
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多