Search code examples
pythonpandasdataframeplotly

create a time series scatter plot with plotly


I would like to create a time series scatter plot with plotly in python, I have for example 2 columns of data "a" and "b" and each entry in the column is a date and time of an event. I want to make a plot where time is on x-axis, and y axis just shows what column data is from, and all data points from the same column would be on one horizontal line.

I'm not sure how to plot this because there are no values for the y axis, I have created something close to what I want in the code below, by creating value columns 'value_a' and 'value_b' and setting values of all points in column "a" to 1 and points in "b" to 2, but this wont work when there are many columns and data points. Thanks

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame(dict(
    a=["2020-01-10", "2020-02-10", "2020-03-10", "2020-04-10", "2020-05-10", "2020-06-10"],
    value_a=[1,1,1,1,1,1],
    b=["2020-01-05", "2020-02-15", "2020-03-18", "2020-08-10", "2021-05-10", "2021-06-10"],
    value_b=[2,2,2,2,2,2]
))


print(df)
fig = go.Figure()
fig.add_trace(go.Scatter(
    mode="markers", x=df["a"], y=df["value_a"],

))
fig.add_trace(go.Scatter(
    mode="markers", x=df["b"], y=df["value_b"],

))

fig.update_xaxes(showgrid=True, ticklabelmode="period")
fig.show()

Solution

  • You can set the y values to the name of the column multiplied by the length of the column:

    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.DataFrame(dict(
        a=["2020-01-10", "2020-02-10", "2020-03-10", "2020-04-10", "2020-05-10", "2020-06-10"],
        b=["2020-01-05", "2020-02-15", "2020-03-18", "2020-08-10", "2021-05-10", "2021-06-10"],
    ))
    fig = go.Figure()
    for col in df.columns:
        fig.add_trace(go.Scatter(
            mode="markers", 
            x=df[col], 
            y=[col] * len(df[col]),
        ))
    fig.update_xaxes(showgrid=True, ticklabelmode="period")
    

    Output:

    enter image description here