1

I want to plot histograms for timedelta64 (example: Timedelta('0 days 00:00:44.749500')

But in both cases, ploty.histogram() does not recognize the correct time but rather displays values (e.g., 50T 60T (See image)).

How do I have to convert the datetime/timedelta that plotly .histogram() recognizes the correct timeaxis? Thanks

fig = px.histogram(x=df_TT_redux["T_delta"],color=df_TT_redux["event_source"],log_y=True)

enter image description here

EDIT: THanks to LittlePanic404 converting to ISO Format gives some interesting results. I guess I have to tweak that still a bit.

using

import isodate
df_TT_redux["T_delta3"]=[isodate.duration_isoformat(x) for x in df_TT_redux["T_delta"]]

fig = px.histogram(x=df_TT_redux["T_delta3"],color=df_TT_redux["event_source"],color_discrete_map=color_discrete_map,log_y=False,log_x=False,nbins=100)

enter image description here

However, another way of solving this could be this:

df_TT_redux["T_delta2"]=df_TT_redux["T_delta"]/pd.Timedelta("1 hour")

or .../pd.Timedelta("1 minute"). Depending on your case

enter image description here

Florida Man
  • 2,021
  • 3
  • 25
  • 43
  • 1
    "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." So you have to convert it in iso format, see https://stackoverflow.com/questions/27168175/convert-a-datetime-timedelta-into-iso-8601-duration-in-python – LittlePanic404 Jun 23 '22 at 09:11
  • 1
    You could also use the `total_seconds` method of `timedelta` to convert it into a numeric type and label the axis accordingly. – Carlos Horn Jun 23 '22 at 10:20

0 Answers0