Skip to content

Commit 1f6cec8

Browse files
committed
Support nano-second level precision x-axis labels.
1 parent dc99dc6 commit 1f6cec8

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

pandas/tseries/plotting.py

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

88
import numpy as np
9-
import datetime
109

1110
from matplotlib import pylab
1211
from pandas.tseries.period import Period
@@ -281,9 +280,19 @@ def _maybe_convert_index(ax, data):
281280
# Patch methods for subplot. Only format_dateaxis is currently used.
282281
# Do we need the rest for convenience?
283282

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
287296

288297

289298
def format_dateaxis(subplot, freq, index):
@@ -319,7 +328,12 @@ def format_dateaxis(subplot, freq, index):
319328

320329
elif isinstance(index, TimedeltaIndex):
321330
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))
323337
subplot.xaxis.set_major_formatter(formatter)
324338
else:
325339
raise IOError('index type not supported')

0 commit comments

Comments
 (0)