Search code examples
pythonpandasplotlyplotly-pythonplotly-express

Plot two traces from two different days to one graph based on x axis


I am trying to display data into one line graph with two traces where on x axis will be time from 00:00:00 to 23:59:59, on y axis 'down_speed' and each trace will be showing data from one day. For exapmle Trace1 from 27th April (00:43:02, 03:43:30, 08:44:13, 18:01:12, 23:32:02) and Trace2 from 28th April(03:02:29, 09:03:07, 18:03:56, 23:04:40).

I have csv where data are stored and sorted by time like this:

    id             time         down_speed  
     1    2020-04-27 00:43:02     4,807  
     2    2020-04-27 03:43:30     5,046  
     3    2020-04-27 08:44:13     2,12  
     4    2020-04-27 18:01:12     4,065  
     5    2020-04-27 23:32:02     4,558  
     6    2020-04-28 03:02:29     4,803
     7    2020-04-28 09:03:07     3,967
     8    2020-04-28 18:03:56     3,617
     9    2020-04-28 23:04:40     5,703

Now I have this code, which is selecting from range of two exact points in time but I cant figure how to separate days and then put them to one graph on same 'x' axis displaying whole day from midnight to midnight.

import pandas as pd
import plotly.express as px
import plotly.offline as py

df  = pd.read_csv('my_file.csv',parse_dates=['time']);

#plot all data between two times
mask = (df['time'] > '2020-04-27 00:00:00') & (df['time'] <= '2020-04-27 23:59:59')
fig = px.line(df.loc[mask], x = 'time', y = 'speed_download')
py.plot(fig)

I am reading documentation to pandas time series but I didnt find anything that would work, any idea before I start doing some brute force solution?


Solution

  • import pandas as pd
    import plotly.graph_objects as go
    
    df = pd.DataFrame({'time': {1: '2020-04-27 00:43:02', 2: '2020-04-27 03:43:30', 3: '2020-04-27 08:44:13', 4: '2020-04-27 18:01:12', 5: '2020-04-27 23:32:02', 6: '2020-04-28 03:02:29', 7: '2020-04-28 09:03:07', 8: '2020-04-28 18:03:56', 9: '2020-04-28 23:04:40'},
                       'down_speed': {1: 4807, 2: 5046, 3: 2120, 4: 4065, 5: 4558, 6: 4803, 7: 3967, 8: 3617, 9: 5703}})
    
    df['date'] = pd.to_datetime(df['time']).dt.date
    df['time'] = pd.to_datetime(df['time']).apply(lambda x: x.replace(year=1990, month=1, day=1))
    
    # extract the list of dates
    dates = list(df['date'].sort_values().unique())
    
    # generate the traces for the first date
    df1 = df[df['date'] == dates[0]]
    
    data = [go.Scatter(x=list(df1['time']),
                       y=list(df1['down_speed']),
                       name=str(dates[0]),
                       mode='markers+lines')]
    
    # add the traces for the subsequent dates
    for date in dates[1:]:
    
        df1 = df[df['date'] == date]
    
        data.append(go.Scatter(x=list(df1['time']),
                               y=list(df1['down_speed']),
                               name=str(date),
                               mode='markers+lines'))
    
    # define the figure layout
    layout = dict(xaxis=dict(range=[df1['time'].min(), df1['time'].max()],
                             tickformat='%H:%M:%S',
                             type='date',
                             autorange=False))
    
    fig = go.Figure(data=data, layout=layout)
    
    fig.show()
    

    enter image description here