how to format the y axis for a timedelta object in plotly express

Question:

I’ve seen several related questions, but none of the solutions so far seem to solve my problem. The problem is, that instead of e.g. "03:00:00" or similar I get 40T as label on the y axis for my timedelta object. It is nicely formatted in pandas, though: 0 days 03:00:00

The output format is either unusable, e.g. when converting to unsorted strings, or the output doesn’t change at all.

I would like to have an easily readable format on the y axis instead of the seconds (?), it will most often be durations of hours and minutes, in a few cases it might be longer than a day (but something like 40:xx:xx meaning 40 hours would be totally fine

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

Asked By: crazysantaclaus

||

Answers:

Plotly does not seem to handle time-delta format as time-series data; the plotly community is discussing how to handle time-delta. In this context, the time format can be applied by giving the base date of the time series to the time delta value as a workaround.
However, this workaround should be used with the understanding that it is not valid for all time delta values.

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

Answered By: r-beginners
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.