Skip to content

Commit efe5636

Browse files
committed
Adds custom plot formatting for TimedeltaIndex.
1 parent 1f82f18 commit efe5636

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

pandas/tools/plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ def _ts_plot(cls, ax, x, data, style=None, **kwds):
17801780

17811781
lines = cls._plot(ax, data.index, data.values, style=style, **kwds)
17821782
# set date formatter, locators and rescale limits
1783-
format_dateaxis(ax, ax.freq)
1783+
format_dateaxis(ax, ax.freq, data.index)
17841784
return lines
17851785

17861786
def _get_stacking_id(self):

pandas/tseries/plotting.py

+37-19
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
# TODO: Use the fact that axis can have units to simplify the process
77

88
import numpy as np
9+
import datetime
910

1011
from matplotlib import pylab
1112
from pandas.tseries.period import Period
1213
from pandas.tseries.offsets import DateOffset
1314
import pandas.tseries.frequencies as frequencies
1415
from pandas.tseries.index import DatetimeIndex
16+
from pandas.tseries.period import PeriodIndex
17+
from pandas.tseries.tdi import TimedeltaIndex
1518
from pandas.formats.printing import pprint_thing
1619
import pandas.compat as compat
1720

@@ -49,7 +52,7 @@ def tsplot(series, plotf, ax=None, **kwargs):
4952
lines = plotf(ax, series.index._mpl_repr(), series.values, **kwargs)
5053

5154
# set date formatter, locators and rescale limits
52-
format_dateaxis(ax, ax.freq)
55+
format_dateaxis(ax, ax.freq, series.index)
5356
return lines
5457

5558

@@ -278,8 +281,11 @@ def _maybe_convert_index(ax, data):
278281
# Patch methods for subplot. Only format_dateaxis is currently used.
279282
# Do we need the rest for convenience?
280283

284+
def timeTicks(x, pos):
285+
d = datetime.timedelta(seconds=int(x/1e9))
286+
return str(d)
281287

282-
def format_dateaxis(subplot, freq):
288+
def format_dateaxis(subplot, freq, index):
283289
"""
284290
Pretty-formats the date axis (x-axis).
285291
@@ -288,26 +294,38 @@ def format_dateaxis(subplot, freq):
288294
default, changing the limits of the x axis will intelligently change
289295
the positions of the ticks.
290296
"""
291-
majlocator = TimeSeries_DateLocator(freq, dynamic_mode=True,
292-
minor_locator=False,
293-
plot_obj=subplot)
294-
minlocator = TimeSeries_DateLocator(freq, dynamic_mode=True,
295-
minor_locator=True,
296-
plot_obj=subplot)
297-
subplot.xaxis.set_major_locator(majlocator)
298-
subplot.xaxis.set_minor_locator(minlocator)
299-
300-
majformatter = TimeSeries_DateFormatter(freq, dynamic_mode=True,
297+
298+
if isinstance(index, PeriodIndex):
299+
300+
majlocator = TimeSeries_DateLocator(freq, dynamic_mode=True,
301301
minor_locator=False,
302302
plot_obj=subplot)
303-
minformatter = TimeSeries_DateFormatter(freq, dynamic_mode=True,
303+
minlocator = TimeSeries_DateLocator(freq, dynamic_mode=True,
304304
minor_locator=True,
305305
plot_obj=subplot)
306-
subplot.xaxis.set_major_formatter(majformatter)
307-
subplot.xaxis.set_minor_formatter(minformatter)
306+
subplot.xaxis.set_major_locator(majlocator)
307+
subplot.xaxis.set_minor_locator(minlocator)
308+
309+
majformatter = TimeSeries_DateFormatter(freq, dynamic_mode=True,
310+
minor_locator=False,
311+
plot_obj=subplot)
312+
minformatter = TimeSeries_DateFormatter(freq, dynamic_mode=True,
313+
minor_locator=True,
314+
plot_obj=subplot)
315+
subplot.xaxis.set_major_formatter(majformatter)
316+
subplot.xaxis.set_minor_formatter(minformatter)
317+
318+
# x and y coord info
319+
subplot.format_coord = lambda t, y: (
320+
"t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y))
321+
322+
pylab.draw_if_interactive()
323+
324+
elif isinstance(index, TimedeltaIndex):
325+
from matplotlib import ticker
326+
formatter = ticker.FuncFormatter(timeTicks)
327+
subplot.xaxis.set_major_formatter(formatter)
328+
else:
329+
raise IOError('index type not supported')
308330

309-
# x and y coord info
310-
subplot.format_coord = lambda t, y: (
311-
"t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y))
312331

313-
pylab.draw_if_interactive()

0 commit comments

Comments
 (0)