SiHodgy007
asked on
Does anyone know how to plot Timedelta in matplotloib?
i'm looking to plot Timedelta in a bar graph using matplotlib from Pandas
matplot lib see's these as strings and won't plot them?
[Timedelta('0 days 08:43:07'),
Timedelta('0 days 04:43:07'),
Timedelta('0 days 03:43:07')]
matplot lib see's these as strings and won't plot them?
Are you trying to convert strings like '0 days 08:43:07' into timedelta/time objects and then chart them?
ASKER
Exactly that
There's probably better ways to extract the times but this seems to work.
import matplotlib.pyplot as plt
from datetime import timedelta as td
strtimes = ['0 days 08:43:07','0 days 04:43:07','0 days 03:43:07']
times = [st[7:].split(":") for st in strtimes]
times = [td(hours=int(h), minutes=int(m), seconds=int(s)) for h,m,s in times]
print(times)
p1 = plt.bar([1,2,3],[int(t.seconds) for t in times])
plt.show()
ASKER
Thanks Norie, this is close. I get a TypeError: 'Timedelta' object is not subscriptable as it's a column extract of times from a dataframe.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Norie, this worked well.