Search code examples
pythonmatplotlibplotly-pythontimeline

How to create a timeline chart


Does anyone know if it's possible to create a graph like this, with some Python library? If so, does anyone have any examples to share?

enter image description here


Solution

  • It should be pretty straight forward using plotly.express.timeline. Look at the first example on that page. If you want it on one y-axis position like your screenshot then try

    # example directly from Plotly
    import plotly.express as px
    import pandas as pd
    
    df = pd.DataFrame([
        dict(Task="Job A", Start='2009-01-01', Finish='2009-01-03'),
        dict(Task="Job B", Start='2009-01-03', Finish='2009-01-16'),
        dict(Task="Job C", Start='2009-01-16', Finish='2009-01-18')
    ])
    # add color param and remove the y param from the example
    fig = px.timeline(df, x_start="Start", x_end="Finish", color='Task')
    fig.show()
    

    enter image description here