简体   繁体   English

Plotly python axix 代码格式问题与具有毫秒的纪元时间

[英]Plotly python axix ticker format problems with epoch time that has milliseconds

my x axis values are in epoch time with milliseconds.我的 x 轴值是以毫秒为单位的纪元时间。 When I try to set the xaxis ticker to a more readable format such as:当我尝试将 xaxis 代码设置为更易读的格式时,例如:

fig.update_layout(
            xaxis = {'type': 'date','tickformat': '%H:%M:%S'},
            )

The date doesn't come through correctly as expected.日期未按预期正确通过。 The year, day, hour, are all incorrect.年、日、时,全都不对。 I cannot find the proper way to do this with plotly python.我找不到使用 plotly python 执行此操作的正确方法。 If someone could provide an example that would be great.如果有人可以提供一个很好的例子。

Haven't had much luck with millisecond timestamps either.毫秒时间戳也没有太多运气。 One solution could be to pre-format before going to Plotly.一种解决方案是在转到 Plotly 之前进行预格式化。 This slows down performance, but there are certain formats Plotly recognizes.这会降低性能,但 Plotly可以识别某些格式。

Not assuming you are using pandas as your inputs, but you can build the format like so:不假设您使用 pandas 作为输入,但您可以像这样构建格式:

        df2[xaxis_name] = pd.to_datetime(df[xaxis_name], unit='ms').dt.date

https://plotly.com/python/time-series/ https://plotly.com/python/time-series/

Then you can further refine it with:然后你可以进一步细化它:

https://plotly.com/python/reference/#layout-xaxis-tickformathttps://plotly.com/python/reference/#layout-xaxis-tickformat

Such as:如:

fig.update_xaxes(
           tickformat = "%b-%Y"
        )

Worked for me.为我工作。

在此处输入图像描述

Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they're a date pandas column or datetime NumPy array.当相应的数据是 ISO 格式的日期字符串或日期 pandas 列或日期时间 Z3B7F949B2343F9E53970E29F6EF5E1 数组时,Plotly 会自动将轴类型设置为日期格式。

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

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