Line drawn in middle of chart (.add_vline)

Hi,
I am trying to add a vertical line using the (.add_vline) method. However, the line is consistently being plotted in the middle of the chart despite the (x) variable value does not place the line in the middle of the chart. Here is the full code to plot my chart:

Plot the time series data and two-week rolling average using Plotly

fig = px.line(filtered_df, x=‘Time’, y=parameter, title=‘Time Series Plot with Four-Week Rolling Average’)
fig.add_scatter(x=time, y=avg_param, name=‘Four-Week Average’, line=dict(color=‘red’, dash=‘dash’))
fig.update_traces(line=dict(shape=‘spline’))

Add vertical lines for start_date and end_date

fig.add_vline(x = inc_time, line_color= ‘black’, line_dash = ‘dash’)

Update the layout to adjust the title position

fig.update_layout(
title=dict(text='Time Series Plot with Four-Week Rolling Average: ’ + parameter + ’ - ’ + facility, x=0.5, y=0.95),
xaxis=dict(title=‘Time’),
yaxis=dict(title=parameter)
)

fig.show()

Note, here in my case, inc_time is equal to Timestamp(‘2022-01-21 15:45:00’), however, as shown in the chart below, the vertical line is drawn at 17:00.


What am I missing?

Hossam

I was able to find out my flaw and posting here in case anyone else faces the same issue. If using time data, your X value for which you want to plot a vertical line must be of the same type as the X-axis. In my case, the X value for inc_time was of datetime type, while the X axis is of datetime.time. If your data entries use date and time, you can extract the time using the .strptime() method.

1 Like