8

I want to plot a dataframe, which has a timedelta64 index with the index on the x-axis.

The index looks like this:

In [75]: test.index
Out[75]: 
TimedeltaIndex([       '00:00:00', '00:00:00.020000', '00:00:00.040000',
                '00:00:00.060000', '00:00:00.080000', '00:00:00.100000',
                '00:00:00.120000', '00:00:00.140000', '00:00:00.160000',
                '00:00:00.180000',
                ...
                '07:29:31.660000', '07:29:31.680000', '07:29:31.700000',
                '07:29:31.720000', '07:29:31.740000', '07:29:31.760000',
                '07:29:31.780000', '07:29:31.800000', '07:29:31.820000',
                '07:29:31.840000'],
               dtype='timedelta64[ns]', name='master', length=923486, freq=None)

When I plot this dataframe simply with:

plt.plot(test)

I would expect to get the timedeltaindex on the x-axis. However, instead I'm just getting this on the x-axis:

enter image description here

How can I get my TimedeltaIndex on the x-axis instead?

Frank
  • 149
  • 2
  • 8

1 Answers1

7

First of all you may use pandas directly to plot the data. I.e. instead of plt.plot(df) you can use

df.plot()

This would show the correct timedelta on the axis.

If for any reason you need to use matplotlib for plotting, e.g. because you have unequally spaced timestamps, you need to convert the TimedeltaIndex to an absolute datetime. Choosing some (arbitrary) start you may add the deltas to the start to get some absolute datetime.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

index = pd.to_timedelta(np.arange(50,100), unit='s')

df = pd.DataFrame(np.arange(50), index=index)

start=pd.Timestamp('20180606')
plt.plot(start+df.index, df)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 1
    Thanks a lot! That's just what I was looking for. If anyone else is looking for the same thing, this is how I got it to look like what I need: `start = pd.Timestamp('20180606') new_index = start + test.index test.index = new_index index_plot = new_index[::20000] # to reduce the number of labels on x-axis labels = index_plot.strftime('%H:%M:%S') plt.plot(test) plt.xticks(index_plot, labels, rotation=60) plt.show()` That's also kind of a workaround, but I works perfectly for me. – Frank Jun 07 '18 at 07:19
  • `df.plot()` doesn't work, either. It shows a label for the axis, but doesn't show any labels for the ticks – endolith Oct 24 '19 at 15:38
  • @endolith Interesting, but not helpful. In which version of matplotlib and pandas does which code not work? (Best use a new question for that.) – ImportanceOfBeingErnest Oct 24 '19 at 16:49
  • @ImportanceOfBeingErnest pandas 0.25.2 and matplotlib 3.1.1 – endolith Oct 24 '19 at 19:32
  • 1
    @endolith Replacing `plt.plot(...)` by `df.plot()` in the code from this answer and running it with matplotlib 0.25.2 and matplotlib 3.1.1 gives [this figure](https://i.stack.imgur.com/MoBbd.png). If you see something different, or have another problem, I again suggest to create a new question with a [mcve]. – ImportanceOfBeingErnest Oct 24 '19 at 20:08
  • @ImportanceOfBeingErnest Weird. `index = pd.to_timedelta([50, 51, 52], unit='s')` works but `index = pd.to_timedelta([50, 51, 53], unit='s')` doesn't – endolith Oct 24 '19 at 20:30
  • 1
    @endolith That's because pandas does not consider unequally spaced values as time series. It therefore falls back to using matplotlib; but matplotlib does not understand timedeltas. So in that case you really need to use matplotlib. – ImportanceOfBeingErnest Oct 24 '19 at 20:35