Plotting time delta time difference list on y-axis matplotlib line graph

Viewed 129

I am trying to plot a line graph in matplotlib using two lists.

The first list that I have for the x axis is

x = [1,2,3,4,5,6,7,8,9,10]

The next list that I have for the y axis is calculated time difference for each x axis value from a pandas DF of column of datetime objects.

I got this list from doing this

    diff_layers_sd['Difference'] = diff_layers_sd['ValueTime'].diff()

This gave me a list populated with timedelta objects

y = [Timedelta('0 days 00:04:31'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:02'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:01'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:01'), Timedelta('0 days 00:04:16')]

I converted this column into a list and then tried to plot it but my plot is empty. Is there a way to plot Timedelta or to format my data? I would prefer for it to be just in MM:SS on the y-axis

code:

fig, ax = plt.subplots(nrows=13, ncols=1, figsize=(20,20),sharex=False)
...
ax[i].plot(x,y, label=title)
ax[i].yaxis_date()
ax[i].yaxis.set_major_formatter(mdates.DateFormatter("%M:%S")
1 Answers

We can do

s = diff_layers_sd['ValueTime'].diff().dt.total_seconds()
diff_layers_sd['Difference'] = (s//60).astype(str) +":" +(s%60).astype(str) 
Related