pythondata-visualizationplotly-python

Plotly, Python. Can i plot vertical line based on a datetime object?


I am forecasting Covid cases and using Plotly for visualization. I would like to plot a straight vertical line in place where forecast starts. I have a chart like this. chart. I just want to plot a vertical line on date 25 Jan 2021, so it is visible where forecast starts.


Solution

  • because you didn't share your data i tried to solve the answer with a sample code snippet from plotly:

    import plotly.express as px
    
    df = px.data.stocks()
    fig = px.line(df, x='date', y="GOOG")
    
    fig.add_vline(x='2019-01-25')
    fig.show()
    

    I have added the following line to my code before fig.show():

    fig.add_vline(x='2021-01-25')
    

    If my date format differs from yours, you get it by printing your graph input:

    print(df)  
               date  ...
    0    2018-01-01  ...
    1    2018-01-08  ...
    2    2018-01-15  ...
    3    2018-01-22  ...
    4    2018-01-29  ...
    ...      ...      
    

    If you need more info and examples check: https://plotly.com/python/horizontal-vertical-shapes/

    My result chart