Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

data visualization - 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.

question from:https://stackoverflow.com/questions/65942511/plotly-python-can-i-plot-vertical-line-based-on-a-datetime-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...