|
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 |
10 | 9 |
|
11 | 10 | from matplotlib import pylab
|
12 | 11 | from pandas.tseries.period import Period
|
@@ -281,9 +280,19 @@ def _maybe_convert_index(ax, data):
|
281 | 280 | # Patch methods for subplot. Only format_dateaxis is currently used.
|
282 | 281 | # Do we need the rest for convenience?
|
283 | 282 |
|
284 |
| -def timeTicks(x, pos): |
285 |
| - d = datetime.timedelta(seconds=int(x / 1e9)) |
286 |
| - return str(d) |
| 283 | +def timeTicks(x, pos, n_decimals): |
| 284 | + ''' Convert seconds to 'D days HH:MM:SS.F' ''' |
| 285 | + s, ns = divmod(x, 1e9) |
| 286 | + m, s = divmod(s, 60) |
| 287 | + h, m = divmod(m, 60) |
| 288 | + d, h = divmod(h, 24) |
| 289 | + decimals = int(ns * 10**(n_decimals - 9)) |
| 290 | + s = r'{:02d}:{:02d}:{:02d}'.format(int(h), int(m), int(s)) |
| 291 | + if n_decimals > 0: |
| 292 | + s += '.{{:0{:0d}d}}'.format(n_decimals).format(decimals) |
| 293 | + if d != 0: |
| 294 | + s = '{:d} days '.format(int(d)) + s |
| 295 | + return s |
287 | 296 |
|
288 | 297 |
|
289 | 298 | def format_dateaxis(subplot, freq, index):
|
@@ -319,7 +328,12 @@ def format_dateaxis(subplot, freq, index):
|
319 | 328 |
|
320 | 329 | elif isinstance(index, TimedeltaIndex):
|
321 | 330 | from matplotlib import ticker
|
322 |
| - formatter = ticker.FuncFormatter(timeTicks) |
| 331 | + (vmin, vmax) = tuple(subplot.xaxis.get_view_interval()) |
| 332 | + n_decimals = int(np.ceil(np.log10(100 * 1e9 / (vmax - vmin)))) |
| 333 | + if n_decimals > 9: |
| 334 | + n_decimals = 9 |
| 335 | + formatter = ticker.FuncFormatter( |
| 336 | + lambda x, pos: timeTicks(x, pos, n_decimals)) |
323 | 337 | subplot.xaxis.set_major_formatter(formatter)
|
324 | 338 | else:
|
325 | 339 | raise IOError('index type not supported')
|
|
0 commit comments