Px.timeline: Vertical Line to indicate certain date

Hi guys, the following code is taken from the docs (Gantt Charts | Python | Plotly) and slightly adapted for my purpose:

import plotly.express as px
import pandas as pd


df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", template='plotly_white')
fig.update_layout(showlegend=False, xaxis_tickformat='%y-%m-%d')
fig.update_xaxes(showgrid=False, showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(showticklabels=False, title=None, autorange="reversed")
fig.show()

date = '2009-04-01'

As you can see in the code, I intentionally removed all grid lines. However I would like to display one single line to indicate a certain date like below:

Does anyone know how to do this?

Thank you very much in advance!

Hi @HansPeter123,

You can make use of the fig.add_vline() for this. More info on this page.

The modified code would be:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])

date = '2009-04-01'

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", template='plotly_white')
fig.add_vline(x=date, line_width=3, line_color="black")
fig.update_layout(showlegend=False, xaxis_tickformat='%y-%m-%d')
fig.update_xaxes(showgrid=False, showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(showticklabels=False, title=None, autorange="reversed")
fig.show()

1 Like

Great! Thank you very much :slight_smile: