Search code examples
pythonpandasplotly

How to add multiple trace on the same plot with Plotly?


How to add multiple trace on the same graph with plotly express? The x-axis to be "dates" and the y-axis to be "values". Each "id" will have its own trace on the graph. I tried plotting a trace with the first id and intend to addon others to the graph but unsuccessful. My code:

import plotly.express as px

fig = px.bar(dfx, x=dfx.date[0][0], y=dfx.value[0][0])   # plot first row of the dataframe
fig.show()

Sample Dataframe

Sample dataframe in dict format:

[{'id': '37f883e7e4b47b9a463bd00a',
  'time_mins': [34.2, 24.6, 24.7, 19.9, 23.8, 18.9, None],
  'value': [-19.35, 10.21, 9.9, 24.68, 12.67, 27.76, None],
  'date': [datetime.datetime(2017, 8, 14, 6, 32, 1, 851000),
   datetime.datetime(2017, 8, 21, 5, 54, 11, 711000),
   datetime.datetime(2017, 8, 25, 11, 50, 22, 201000),
   datetime.datetime(2017, 8, 28, 5, 35, 9, 886000),
   datetime.datetime(2017, 9, 1, 11, 40, 22, 159000),
   datetime.datetime(2017, 9, 11, 6, 25, 16, 110000),
   None]},
 {'id': '37eba6db0eed3488860b07ed',
  'time_mins': [23.7, 21.0, 23.0, 19.7, 21.0, 21.1, 20.1, 20.0],
  'value': [12.98, 21.29, 15.13, 25.29, 21.29, 20.98, 24.06, 24.37],
  'date': [datetime.datetime(2017, 8, 4, 12, 21, 1, 422000),
   datetime.datetime(2017, 8, 11, 12, 0, 57, 744000),
   datetime.datetime(2017, 8, 18, 11, 54, 11, 655000),
   datetime.datetime(2017, 8, 20, 2, 12, 4, 759000),
   datetime.datetime(2017, 8, 27, 2, 4, 24, 745000),
   datetime.datetime(2017, 8, 28, 6, 10, 17, 230000),
   datetime.datetime(2017, 9, 8, 12, 1, 8, 540000),
   datetime.datetime(2017, 9, 15, 11, 30, 57, 29000)]},
 {'id': '37f0eccc09f3193bb0d63725',
  'time_mins': [16.8, 17.4, 17.6, 17.4, 16.4],
  'value': [34.22, 32.38, 31.76, 32.38, 35.45],
  'date': [datetime.datetime(2017, 8, 8, 12, 14, 48, 844000),
   datetime.datetime(2017, 8, 15, 11, 37, 18, 1000),
   datetime.datetime(2017, 8, 22, 11, 43, 55, 218000),
   datetime.datetime(2017, 8, 29, 11, 40, 29, 524000),
   datetime.datetime(2017, 9, 5, 11, 50, 23, 651000)]},
 {'id': '6303630c067719cf5bb7c98e',
  'time_mins': [21.2, None, 23.0],
  'value': [20.68, None, 15.13],
  'date': [datetime.datetime(2017, 8, 22, 12, 9, 44, 371000),
   None,
   datetime.datetime(2017, 9, 12, 11, 47, 10, 55000)]},
 {'id': '37e905f5ba0a4ac5b6b26ee0',
  'time_mins': [19.0, 18.1, 17.8, 14.5, 13.8],
  'value': [27.45, 30.22, 31.14, 41.3, 43.46],
  'date': [datetime.datetime(2017, 8, 2, 11, 45, 5, 908000),
   datetime.datetime(2017, 8, 4, 11, 30, 25, 236000),
   datetime.datetime(2017, 8, 8, 11, 36, 59, 697000),
   datetime.datetime(2017, 8, 11, 11, 30, 28, 737000),
   datetime.datetime(2017, 8, 13, 1, 22, 59, 471000)]}]

Solution

  • In such cases, the data must first be preprocessed: the column list data must be expanded into rows using df.explore(). The resulting data frame can then be graphed by id by drawing a graph with the data frames extracted based on the id. The width of the bar graph is very narrow because the time series of the original data is handled down to milliseconds. To improve this, the data was changed to date. If you need milliseconds, please add a range slider.

    dfx = df.explode(['time_mins','value', 'date'])  [EDIT]
    
    import plotly.graph_objects as go
    
    fig = go.Figure()
    for i in dfx['id'].unique():
        dff = dfx.query('id == @i')
        fig.add_trace(go.Bar(x=dff['date'].dt.date, y=dff.value, name=i))
        #fig.add_trace(go.Bar(x=dff['date'], y=dff.value, name=i))
        
    fig.update_layout(autosize=True, width=1000, bargap=0.01)#xaxis=dict(rangeslider=dict(visible=True)
    fig.update_layout(legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.05,
        xanchor="left",
        x=0.01
    ))
    
    fig.show()
    

    enter image description here

    enter image description here