Link to home
Start Free TrialLog in
Avatar of SiHodgy007
SiHodgy007Flag for United Kingdom of Great Britain and Northern Ireland

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

[Timedelta('0 days 08:43:07'),
Timedelta('0 days 04:43:07'),
Timedelta('0 days 03:43:07')]

Open in new window


matplot lib see's these as strings and won't plot them?
Avatar of Norie
Norie

Are you trying to convert strings like '0 days 08:43:07' into timedelta/time objects and then chart them?
Avatar of SiHodgy007

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()

Open in new window

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
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Norie, this worked well.