【问题标题】:Plotly, Python. Can i plot vertical line based on a datetime object?情节,Python。我可以根据日期时间对象绘制垂直线吗?
【发布时间】:2021-01-28 17:45:44
【问题描述】:

我正在预测 Covid 病例并使用 Plotly 进行可视化。我想在预测开始的地方画一条垂直线。我有一个这样的图表。 chart。我只想在 2021 年 1 月 25 日绘制一条垂直线,以便预测开始的位置可见。

【问题讨论】:

    标签: python data-visualization plotly-python


    【解决方案1】:

    因为你没有分享你的数据,所以我试图用来自 plotly 的示例代码 sn-p 来解决答案:

    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()
    

    我在 fig.show() 之前在我的代码中添加了以下行:

    fig.add_vline(x='2021-01-25')
    

    如果我的日期格式与您的不同,您可以通过打印图表输入来获得它:

    print(df)  
               date  ...
    0    2018-01-01  ...
    1    2018-01-08  ...
    2    2018-01-15  ...
    3    2018-01-22  ...
    4    2018-01-29  ...
    ...      ...      
    

    如果您需要更多信息和示例,请查看:https://plotly.com/python/horizontal-vertical-shapes/

    My result chart

    【讨论】:

    • 嘿,有没有办法在图例中添加垂直线,以便我可以打开和关闭它?
    猜你喜欢
    • 1970-01-01
    • 2014-02-24
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多