【问题标题】:Using a custom time format in Plotly Express timeline在 Plotly Express 时间线中使用自定义时间格式
【发布时间】:2021-03-06 21:22:22
【问题描述】:

我正在使用 plotly express 制作自定义时间线图对象,并且我想编辑 x(time) 轴以显示不同的时间格式,但它只允许“日期时间”。有没有办法使用时间线对象在情节中使用自定义时间类型?有没有更好的框架,比如 matplotlib 或者别的什么?

【问题讨论】:

    标签: python python-3.x matplotlib plotly plotly-express


    【解决方案1】:

    可以使用 plotly 自定义时间格式,请参阅plotly documentation。此处显示的示例使用px.line(),但格式与px.timeline() 的使用方式相同。

    例子:

    presidents = pd.DataFrame([{"name": "Clinton", "start": '1993-01-20', 'end': '2001-01-20'},
                               {"name": "Bush", "start": '2001-01-20', 'end': '2009-01-20'},
                               {"name": "Obama", "start": '2009-01-20', 'end': '2017-01-20'},
                               {"name": "Trump", "start": '2017-01-20', 'end': '2021-01-20'}
                              ])
    
    fig = px.timeline(presidents,
                      x_start="start",
                      x_end="end",
                      y="name"
                     )
    fig.update_xaxes(
        dtick="M48",   # display x-ticks every 24 months months
        tickformat="Date:%Y-%m-%d"  # date format
    ) 
    fig.show()
    

    这给出了:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-09
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多