Adding additional text to the hovertext label

最后都变了- 提交于 2019-12-02 04:44:54

You want to set the text or hovertext element for each chart/trace. Both text and hovertext will work here. The reason you may need both can be seen here. You may also want to change the hoverinfo element. Your options are 'x', 'y', 'none', 'text', 'all'. Additional resources are: text and annotations, docs, and python example. In addition, to get the count of cases at a time period I took two different groupby operations and then concatenated them together.

Example using your dataframe:

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)

Similar SO questions are here and here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!