2
votes

I have some series of data, which is a timedelta data type. I wanted to plot these timedelta into a bar diagram where the y axis should only be marked in hours instead of some other format. Previously, when I was trying with a line plot in matplotlib, it showed some not understandable numbers. The following is the sample of my timedelta series of pandas:

date
2020-04-11   0 days 02:00:00
2020-04-12   0 days 03:00:00
2020-04-13   0 days 02:00:00
2020-04-14   0 days 03:00:00
2020-04-15   0 days 01:00:00
2020-04-16   0 days 03:00:00
Freq: D, dtype: timedelta64[ns]

When I am trying to plot it in matplotlib, it results in a plot with y axis values look weird to me. enter image description here

Please help me to work out with the plots, where the y-axis tick labels should be in 01:00, 02:00 like format.

Eagerly waiting for some of the help.

1

1 Answers

2
votes

A possible way is to convert the deltas in seconds and define a FuncFormatter. This is my test series and my final plot:

2020-04-11   02:00:00
2020-04-12   03:00:00
2020-04-13   05:00:00
dtype: timedelta64[ns]
def delta(x, pos):
    out = str(datetime.timedelta(seconds=x)  )
    return out
            
fig = plt.figure()
ax = fig.gca()
form = matplotlib.ticker.FuncFormatter(delta)
ax.yaxis.set_major_formatter(form)
ax.plot(s.index, s/np.timedelta64(1,'s'))
ax.set_yticks(s/np.timedelta64(1,'s'))
ax.set_xticks(s.index)
plt.show()

enter image description here