Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 658
tyonh12 Avatar asked Nov 22 '25 20:11

tyonh12


1 Answers

I managed to make it work with text annotations by converting it back to milliseconds as suggested here

Here is the example code:

import plotly.express as px
import datetime
df = px.data.stocks(indexed=True)
fig = px.line(df)
fig.add_vline(x=datetime.datetime.strptime("2018-09-24", "%Y-%m-%d").timestamp() * 1000, annotation_text="test" )
fig.show()
like image 170
Agustin Barrachina Avatar answered Nov 24 '25 10:11

Agustin Barrachina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!