我已经看到了几个相关的问题,但到目前为止,似乎没有一个解决方案能解决我的问题.问题是,我得到的不是"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()