我已经看到了几个相关的问题,但到目前为止,似乎没有一个解决方案能解决我的问题.问题是,我得到的不是"03:00:00"或类似的"03:00:00",而是我的时间增量对象在y轴上的标签为40T.不过,Pandas 的格式很好:0天03:00:00

输出格式要么不可用,例如在转换为未排序的字符串时,要么输出根本不变.

我希望在y轴上有一个易于阅读的格式,而不是秒(?),它通常是小时和分钟的持续时间,在一些情况下可能超过一天(但类似40:xx:xx的意思是40小时就完全可以了

import pandas as pd
data = [['tom', 10, "2023-06-21 06:23:55+00:00", "2023-06-21 09:23:55+00:00"], ['nick', 15, "2023-06-20 06:23:55+00:00", "2023-06-21 06:23:55+00:00"], ['juli', 14, "2023-06-21 06:23:50+00:00", "2023-06-21 06:23:55+00:00"]]
df = pd.DataFrame(data, columns=['name', 'age', "start", "stop"])
df["start"] = pd.to_datetime(df["start"])
df["stop"] = pd.to_datetime(df["stop"])
df["duration"] = df["stop"] - df["start"]
df["duration"]

#### output
0   0 days 03:00:00
1   1 days 00:00:00
2   0 days 00:00:05
Name: duration, dtype: timedelta64[ns]

import plotly.express as px
import plotly.graph_objects as go

fig = px.scatter(
        df,
        x=df["age"].sort_values(ascending=False),
        y="duration", # I guess it shows the seconds
        #y=pd.to_timedelta(df.duration, unit='h'), # same format as before
        #y=df["duration"].sort_values(ascending=True).dt.to_pytimedelta().astype(str), # fixed label with equal distance between marks, regardless of numerical difference
        color="name",
    )
figure = go.Figure(data=fig)
# figure.update_layout(yaxis_tickformat='%H:%M:%S') # adds a lot of zeroes?
# figure.update_layout(yaxis_tickformat="%H:%M:%S.%f")
figure.show()

plot

推荐答案

Ploly似乎没有将时间增量格式作为时间序列数据来处理;Ploly社区的discussing个主题是如何处理时间增量.在这种情况下,可以通过将时间序列的基准日期指定给时间差值作为解决办法来应用时间格式. 但是,使用此解决方法时应了解,它并非对所有时间增量值都有效.

import plotly.express as px
import plotly.graph_objects as go

fig = px.scatter(
        df,
        x=df["age"].sort_values(ascending=False),
        y=df["duration"] + pd.to_datetime('1970/01/01'), 
        color="name",
    )
figure = go.Figure(data=fig)
figure.update_layout(yaxis_tickformat="%H:%M:%S.%f")
figure.show()

enter image description here

Python相关问答推荐

使用decorator 重复超载

解析讨论论坛只给我第一个用户 comments ,但没有给我其他用户回复

当pip为学校作业(job)安装sourcefender时,我没有收到匹配的分发错误.我已经try 过Python 3.8.10和3.10.11

在Windows上启动新Python项目的正确步骤顺序

为什么dict(id=1,**{id:2})有时会引发KeyMessage:id而不是TypMessage?

ambda将时间戳与组内另一列的所有时间戳进行比较

将HLS纳入媒体包

理解Python的二分库:澄清bisect_left的使用

将整组数组拆分为最小值与最大值之和的子数组

numba jitClass,记录类型为字符串

为什么符号没有按顺序添加?

使可滚动框架在tkinter环境中看起来自然

如何在Python数据框架中加速序列的符号化

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

ThreadPoolExecutor和单个线程的超时

导入...从...混乱

Flash只从html表单中获取一个值

Python—转换日期:价目表到新行

Pandas:填充行并删除重复项,但保留不同的值

BeautifulSoup-Screper有时运行得很好,很健壮--但有时它失败了::可能这里需要一些更多的异常处理?