Home > Software engineering >  Unable to plot traces in plotly
Unable to plot traces in plotly

Time:03-12

I am trying to plot this dataframe:

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

I want 'Sample Name' to be on the x-axis, 'Result' on the y axis, and 'Date Time' to be shown on hover.

I have this code:

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

But for some reason it plots traces on top of each other.. like this:

enter image description here enter image description here

They are literally on top of each other...

Instead, in my case, there is supposed to be one single data point for each Sample Name corresponding to the Result, and labeled as corresponding "Date Time".

Please, help :(

CodePudding user response:

I think the answer is that it's plotting the entire data set for every datetime.

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

Try changing it to something like this:

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

CodePudding user response:

It is possible by simply defining it in the hover text. In this case, if it is time-series data, it must be converted to a string type.

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

enter image description here

  • Related