6
6
# TODO: Use the fact that axis can have units to simplify the process
7
7
8
8
import numpy as np
9
+ import datetime
9
10
10
11
from matplotlib import pylab
11
12
from pandas .tseries .period import Period
12
13
from pandas .tseries .offsets import DateOffset
13
14
import pandas .tseries .frequencies as frequencies
14
15
from pandas .tseries .index import DatetimeIndex
16
+ from pandas .tseries .period import PeriodIndex
17
+ from pandas .tseries .tdi import TimedeltaIndex
15
18
from pandas .formats .printing import pprint_thing
16
19
import pandas .compat as compat
17
20
@@ -49,7 +52,7 @@ def tsplot(series, plotf, ax=None, **kwargs):
49
52
lines = plotf (ax , series .index ._mpl_repr (), series .values , ** kwargs )
50
53
51
54
# set date formatter, locators and rescale limits
52
- format_dateaxis (ax , ax .freq )
55
+ format_dateaxis (ax , ax .freq , series . index )
53
56
return lines
54
57
55
58
@@ -278,8 +281,11 @@ def _maybe_convert_index(ax, data):
278
281
# Patch methods for subplot. Only format_dateaxis is currently used.
279
282
# Do we need the rest for convenience?
280
283
284
+ def timeTicks (x , pos ):
285
+ d = datetime .timedelta (seconds = int (x / 1e9 ))
286
+ return str (d )
281
287
282
- def format_dateaxis (subplot , freq ):
288
+ def format_dateaxis (subplot , freq , index ):
283
289
"""
284
290
Pretty-formats the date axis (x-axis).
285
291
@@ -288,26 +294,38 @@ def format_dateaxis(subplot, freq):
288
294
default, changing the limits of the x axis will intelligently change
289
295
the positions of the ticks.
290
296
"""
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 ,
301
301
minor_locator = False ,
302
302
plot_obj = subplot )
303
- minformatter = TimeSeries_DateFormatter (freq , dynamic_mode = True ,
303
+ minlocator = TimeSeries_DateLocator (freq , dynamic_mode = True ,
304
304
minor_locator = True ,
305
305
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' )
308
330
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 ))
312
331
313
- pylab .draw_if_interactive ()
0 commit comments