无法在情节中绘制痕迹

Unable to plot traces in plotly

我正在尝试绘制此数据框:

Date Time           Sample Name         Result
2020-08-26 14:34:02 QC Test             0.742
2020-08-26 14:55:24 Myoblast            0.506
2020-08-26 15:12:27 Myoblast DEM        0.607
2020-08-27 15:23:10 CD T2 P1            0.854
2020-08-27 15:25:06 CD T2 P2            0.854
2020-08-27 15:27:32 CD T2 P3            0.831 
2020-08-27 15:47:05 Amy F12             0.894
2020-08-27 16:57:02 CD- T2 P8           0.963

我希望 'Sample Name' 在 x 轴上,'Result' 在 y 轴上,'Date Time' 在悬停时显示。

我有这个代码:

layout = go.Layout(title='Viability Plot',
                   xaxis={'title':'Sample Name'},
                   yaxis={'title':'Viability(%)'})
    
    
fig = go.Figure(layout=layout)


for c in df['Date Time']:
    fig.add_trace(go.Scatter(x=df['Sample Name'], y=df['Result'],
                             mode='markers',
                             name = str(c)))

但出于某种原因,它会在彼此之上绘制痕迹。像这样:

它们实际上是在彼此之上...

相反,在我的例子中,每个样本名称应该有一个数据点对应于结果,并标记为相应的“日期时间”。

求求你帮忙:(

我认为答案是它绘制了每个日期时间的整个数据集。

x=df['Sample Name'], y=df['Result']

试着把它改成这样:

for c, n in enumerate(df['Date Time']):
    fig.add_trace(go.Scatter(x=df['Sample Name'][n], y=df['Result'][n],
                         mode='markers',
                         name = str(c)))

只需在悬停文本中定义即可。在这种情况下,如果是time-series数据,则必须转为字符串类型

import pandas as pd
import numpy as np
import io

data = '''
"Date Time"           "Sample Name"         Result
"2020-08-26 14:34:02" "QC Test"             0.742
"2020-08-26 14:55:24" "Myoblast"            0.506
"2020-08-26 15:12:27" "Myoblast DEM"        0.607
"2020-08-27 15:23:10" "CD T2 P1"            0.854
"2020-08-27 15:25:06" "CD T2 P2"            0.854
"2020-08-27 15:27:32" "CD T2 P3"            0.831 
"2020-08-27 15:47:05" "Amy F12"             0.894
"2020-08-27 16:57:02" "CD- T2 P8"           0.963
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
df['Date Time'] = pd.to_datetime(df['Date Time'])

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(x=df['Sample Name'],
                         y=df['Result'],
                         mode='markers',
                         showlegend=False,
                         hovertext=df['Date Time'].astype(str)
                        )
             )

fig.update_layout(title='Viability Plot',
                  xaxis={'title':'Sample Name'},
                  yaxis={'title':'Viability(%)'}
)
fig.show()