【问题标题】:Adding additional text to the hovertext label向 hovertext 标签添加其他文本
【发布时间】:2018-11-27 21:09:15
【问题描述】:

我已经搜索了一段时间,但似乎找不到相关问题。有类似的问题,但没有什么能触及我想要用我拥有的代码做的事情的核心。

我正在尝试在 plotly 中向 hovertext 添加其他文本。到目前为止,这是我的代码:

import pandas as pd
import numpy as np

from plotly.offline import *
init_notebook_mode(connected=True)

graph1 = merged.groupby(['study_arm', 'visit_label'])['mjsn'].mean().unstack('study_arm')
graph1.iplot(mode='lines+markers',
                 symbol=['diamond-open', 'square-open', 'circle-dot', 'hexagon-open'],
                 size=8, colorscale = 'dark2', yTitle='ytitle', xTitle='xtitle',  
                 title='Title of Graph',
                 hoverformat = '.2f')


hv = merged.groupby(['study_arm', 'visit_label']).size()
print(hv)

注意:“合并”是数据框,显示在下面的示例数据中。

上面的代码给出了以下输出(注意:将鼠标悬停在时间点上会给出每个跟踪的一些信息,我拍了一张照片来展示它的样子)。

我的问题是如何从表中获取主题计数到每个时间点的每个跟踪的悬停文本(最好在悬停文本的第二行,看起来像“N = x”,其中 x 是主题编号来自图片中图表下方的表格)。

这是我用来创建此图表和表格的虚拟数据集的示例

subject_number  visit_label mjsn    study_arm
  20001         Day 1        0          B
  20001         Month 06     0.4        B
  20001         Month 12     0.2        B
  20003         Day 1        0          B
  20003         Month 06    -0.9        B
  20003         Month 12    -0.7        B
  20005         Day 1        0          C
  20005         Month 06     0.1        C
  20005         Month 12    -0.1        C
  20007         Day 1        0          D
  20007         Month 06     0          D
  20007         Month 12    -0.3        D
  20008         Day 1        0          C
  20008         Month 06    -0.3        C
  20008         Month 12    -0.1        C
  20010         Day 1        0          A
  20010         Month 06    -0.6        A
  20010         Month 12    -0.4        A

【问题讨论】:

    标签: python-3.x pandas jupyter-notebook plotly


    【解决方案1】:

    您想为每个图表/跟踪设置texthovertext 元素。 text 和 hovertext 都可以在这里使用。您可能需要两者的原因可以看到here。您可能还想更改hoverinfo 元素。您的选择是'x', 'y', 'none', 'text', 'all'。其他资源是:text and annotationsdocspython example。此外,为了获得某个时间段的病例数,我采用了两个不同的groupby 操作,然后将它们连接在一起。

    使用您的数据框的示例:

    import plotly.graph_objs as go
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
    init_notebook_mode(connected=True)
    import pandas as pd
    
    df = pd.DataFrame({
        'subject_number' : [20001, 20001, 20001, 20003, 20003, 20003, 20005, 20005, 
            20005, 20007, 20007, 20007, 20008, 20008, 20008, 20010, 20010, 20010], 
    
        'visit_label' : ['Day 1', 'Month 6', 'Month 12', 'Day 1', 'Month 6', 
            'Month 12', 'Day 1', 'Month 6', 'Month 12', 'Day 1', 'Month 6',  
            'Month 12', 'Day 1', 'Month 6', 'Month 12', 'Day 1', 'Month 6', 
            'Month 12'],  
    
        'mjsn':[0, 0.4, 0.2, 0, -0.9, -0.7, 0, 0.1, -0.1, 0, 0, -0.3, 0, -0.3, -0.1,
            0, -0.6, -0.4], 
    
        'study_arm':['B', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D', 
            'C', 'C', 'C', 'A', 'A', 'A']
        })
    
    
    
    grouped = df.groupby(['study_arm', 'visit_label'])
    tmp1 = grouped.mean()["mjsn"]
    tmp2 = grouped.count()["subject_number"]
    output_df = pd.concat([tmp1, tmp2], axis = 1)
    
    data = []
    for study in output_df.index.get_level_values(0).unique():
        trace = go.Scatter(
            x = output_df.loc[study, :].index,
            y = output_df.loc[study, "mjsn"],
            hovertext= ["msjn:{0}<br>subject:{1}".format(x, int(y)) 
                        for x,y in zip(output_df.loc[study, "mjsn"],
                            output_df.loc[study, "subject_number"])],
            mode = 'lines+markers',
            hoverinfo = 'text'
        )
        data += [trace]
    #
    iplot(data)
    

    类似的 SO 问题是 herehere

    【讨论】:

    • 非常感谢!
    • 不幸的是,这并没有让我到达我需要的地方。研究组的图表中应该只有四条线,我试图在每次访问时获得每个研究组的受试者计数。对不起,我在检查这个作为回答我的问题时跳了枪。我投了赞成票,因为我确实从你的回复中学到了一些东西。
    • 谢谢!我标记为已回答,非常感谢您的帮助。您是否有机会帮助我了解如何将“study_arm”(A、B、C、D)作为跟踪标签而不是 0、1、2、3?
    • 谢谢!我现在会让你一个人呆着,但非常感谢你的帮助!!
    • 我不想问这个问题,但我花了三个小时试图弄清楚将 msjn 数字四舍五入为两位数。你给我的这种格式很棒,但我过去学到的东西似乎不起作用(即 hoverformat = '.2f')。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多