简体   繁体   English

Plotly,Python。 我可以基于日期时间 object 的 plot 垂直线吗?

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

I am forecasting Covid cases and using Plotly for visualization.我正在预测 Covid 病例并使用 Plotly 进行可视化。 I would like to plot a straight vertical line in place where forecast starts.我想 plot 在预测开始的地方画一条垂直线。 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.我只想在 2021 年 1 月 25 日的日期为 plot 一条垂直线,因此可以看到预测开始的位置。

because you didn't share your data i tried to solve the answer with a sample code snippet from plotly:因为您没有共享您的数据,所以我尝试使用 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.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/如果您需要更多信息和示例,请查看: https://plotly.com/python/horizontal-vertical-shapes/

My result chart我的结果图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM