-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetick.py
More file actions
514 lines (475 loc) · 19.8 KB
/
datetick.py
File metadata and controls
514 lines (475 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import warnings
from datetime import datetime
import numpy as np
import matplotlib.dates as mpld
import matplotlib
if matplotlib.get_backend() == 'MacOSX':
# With MacOSX backend, draw() does not update the ticks
# See warning at
# https://matplotlib.org/3.3.0/tutorials/advanced/blitting.html#sphx-glr-tutorials-advanced-blitting-py
import sys
if sys.version_info[0:2] < (3, 6):
#warnings.filterwarnings("ignore", '.*backend.*', category=UserWarning)
#the above should work and is better because more specific.
warnings.simplefilter("ignore", category=UserWarning)
gui_env = ['Qt5Agg','QT4Agg','GTKAgg','TKAgg','WXAgg']
for gui in gui_env:
try:
#print('Trying ' + gui)
matplotlib.use(gui, force=True)
import matplotlib.pyplot as plt
#print('Success with ' + gui)
break
except:
#print('Failure with ' + gui)
continue
else:
try:
import matplotlib.pyplot as plt
except:
#print('Failed: import matplotlib.pyplot as plt')
gui_env = ['Qt5Agg','QT4Agg','GTKAgg','TKAgg','WXAgg']
for gui in gui_env:
try:
matplotlib.use(gui, force=True)
import matplotlib.pyplot as plt
break
except:
continue
def datetick(dir, **kwargs):
'''
datetick('x') or datetick('y') formats the major and minor tick labels
of the current figure.
datetick('x', axes=ax) or datetick('y', axes=ax) formats the given
axes `ax`.
Example:
--------
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
from hapiclient.plot.datetick import datetick
d1 = dt.datetime(1900, 1, 2)
d2 = dt.datetime.fromordinal(10 + dt.datetime.toordinal(d1))
x = np.array([d1, d2], dtype=object)
y = [0.0,1.0]
plt.clf()
plt.plot(x, y)
datetick('x')
'''
# Based on spacepy/plot/utils.py on 07/10/2017, but many additions.
# TODO: Account for leap years instead of using 367, 366, etc.
# TODO: Use numsize() to determine if figure width and height
# will cause overlap when default number major tick labels is used.
# TODO: If time[0].day > 28, need to make first tick at time[0].day = 28
# as needed.
# TODO: If first data point has fractional seconds, the plot won't have
# a major x-label right below it. This is due to the fact that
# MicrosecondLocator() does not take a keyword argument of
# "bymicroseconds".
# TODO: Adjust lower and upper limits as in 366*8 span
def millis(x, pos):
x = matplotlib.dates.num2date(x)
label = x.strftime('.%f')
label = label[0:3]
#label = label.rstrip(".")
return label
DOPTS = {}
DOPTS.update({'debug': False})
DOPTS.update({'set_cb': True})
DOPTS.update({'axes': None})
# Override defaults
for key, value in kwargs.items():
if key in DOPTS:
DOPTS[key] = value
else:
print('Warning: Keyword option "%s" is not valid.' % key)
if 'axes' in kwargs:
axes = kwargs['axes']
fig = axes.figure
else:
axes = plt.gca()
fig = plt.gcf()
debug = DOPTS['debug']
def on_xlims_change(ax): datetick('x', axes=ax, set_cb=False)
def on_ylims_change(ax): datetick('y', axes=ax, set_cb=False)
def draw(fig): fig.canvas.draw()
bbox = axes.dataLim
if dir == 'x':
datamin = bbox.x0
datamax = bbox.x1
lim = axes.get_xlim()
else:
datamin = bbox.y0
datamax = bbox.y1
lim = axes.get_ylim()
try:
mpld.num2date(lim[0])
except:
raise ValueError('timeseries(): Lower axis limit of %f is not a valid Matplotlib datenum' % lim[0])
try:
mpld.num2date(lim[1])
except:
raise ValueError('timeseries(): Upper axis limit of %f is not a valid Matplotlib datenum' % lim[1])
try:
mpld.num2date(datamin)
except:
raise ValueError('timeseries(): Minimum data value of %f is not a valid Matplotlib datenum' % datamin)
try:
mpld.num2date(datamax)
except:
raise ValueError('timeseries(): Maximum data value of %f is not a valid Matplotlib datenum' % datamax)
tmin = np.max((lim[0], datamin))
tmax = np.min((lim[1], datamax))
time = mpld.num2date((tmin,tmax))
if debug == True:
print('Data min time: %f' % datamin)
print('Data max time: %f' % datamax)
if datamin == datamax:
axes.set_xticks([time[0]])
axes.set_xticklabels([datetime.strftime(time[0],'%Y-%m-%dT%H:%M:%S')])
return
deltaT = time[-1] - time[0]
nDays = deltaT.days
nHours = deltaT.days * 24.0 + deltaT.seconds/3600.0
nSecs = deltaT.total_seconds()
if debug == True:
print("Total seconds: %s" % deltaT.total_seconds())
# fmt is format of the tick labels
# fmt2 contains additional information that is used for the first tick label
# or when there is a majore change. For example, if
# fmt = %M:%S and fmt2 = %H, the labels will have only minute and hour and
# the first tick will have a label of %M:%S\n%H. If there is a change
# in hour somewhere on the axis, that label will include the new hour.
# Note that interval=... is specified even when it would seem to be
# redundant. It is needed to workaround the bug discussed at
# https://stackoverflow.com/questions/31072589/matplotlib-date-ticker-exceeds-locator-maxticks-error-for-no-apparent-reason
if deltaT.total_seconds() < 0.1:
# < 0.1 second
# At this level of zoom, would need original datetime data
# which has not been converted by date2num and then re-plot
# using integer number of milliseconds. E.g., call
# plotd(dtobj,y)
# where
# t = dtobj converted to milliseconds since first array value
# plotd() calls
# plot(t,y)
# and then makes first label indicate %Y-%m-%dT%H:%M:%S
warnings.warn("Warning: Cannot create accurate time labels with this time resolution.")
# This does not locate microseconds.
Mtick = mpld.MicrosecondLocator(interval=10000)
mtick = mpld.MicrosecondLocator(interval=2000)
from matplotlib.ticker import FuncFormatter
fmt = FuncFormatter(millis)
fmt2 = '%H:%M:%S\n%Y-%m-%d'
if deltaT.total_seconds() < 0.5:
# < 0.5 seconds
# Locators don't locate at this resolution.
# Need to do this manually. See comment above.
warnings.warn("Warning: Cannot create accurate time labels with this time resolution.")
Mtick = mpld.MicrosecondLocator(interval=50000)
mtick = mpld.MicrosecondLocator(interval=10000)
from matplotlib.ticker import FuncFormatter
fmt = FuncFormatter(millis)
fmt2 = '%H:%M:%S\n%Y-%m-%d'
if deltaT.total_seconds() < 1:
# < 1 second
# https://matplotlib.org/api/dates_api.html#matplotlib.dates.MicrosecondLocator
# MircosecondLocator() does not have a "bymicrosecond" option. If
# First point is not at zero microseconds, it won't be labeled.
Mtick = mpld.MicrosecondLocator(interval=100000)
mtick = mpld.MicrosecondLocator(interval=20000)
from matplotlib.ticker import FuncFormatter
fmt = FuncFormatter(millis)
#fmt = mpld.DateFormatter('%M:%S.%f')
fmt2 = '%H:%M:%S\n%Y-%m-%d'
elif deltaT.total_seconds() < 5:
# < 5 seconds
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1)) )
mtick = mpld.MicrosecondLocator(interval=200000)
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 10:
# < 10 seconds
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1)) )
mtick = mpld.MicrosecondLocator(interval=500000)
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 20:
# < 20 seconds
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 2)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 30:
# < 30 seconds
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 5)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 1)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60:
# < 1 minute
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 10)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 2)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60*2:
# < 2 minutes
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 20)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 5)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60*3:
# < 3 minutes
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 20)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 5)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60*5:
# < 5 minutes
Mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 30)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 10)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60*10:
# < 10 minutes
Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 1)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 15)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60*20:
# < 20 minutes
Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 2)) )
mtick = mpld.SecondLocator(bysecond=list(range(0, 60, 30)) )
fmt = mpld.DateFormatter('%M:%S')
fmt2 = '%Y-%m-%dT%H'
elif deltaT.total_seconds() < 60*30:
# < 30 minutes
Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 5)) )
mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 1)) )
fmt = mpld.DateFormatter('%H:%M')
fmt2 = '%Y-%m-%d'
elif deltaT.total_seconds() < 60*60:
# < 60 minutes
Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 10)) )
mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 2)) )
fmt = mpld.DateFormatter('%H:%M')
fmt2 = '%Y-%m-%d'
elif nHours < 2:
Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 15)) )
mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 5)) )
fmt = mpld.DateFormatter('%H:%M')
fmt2 = '%Y-%m-%d'
elif nHours < 4:
Mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 20)) )
mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 5)) )
fmt = mpld.DateFormatter('%H:%M')
fmt2 = '%Y-%m-%d'
elif nHours < 6:
Mtick = mpld.HourLocator(byhour=list(range(0,24,1)) )
mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 10)) )
fmt = mpld.DateFormatter('%H:%M')
fmt2 = '%Y-%m-%d'
elif nHours < 12:
Mtick = mpld.HourLocator(byhour=list(range(0,24,2)) )
mtick = mpld.MinuteLocator(byminute=list(range(0, 60, 30)) )
fmt = mpld.DateFormatter('%H:%M')
fmt2 = '%Y-%m-%d'
elif nHours < 24:
# < 1 day
Mtick = mpld.HourLocator(byhour=list(range(0, 24, 3)) )
mtick = mpld.HourLocator(byhour=list(range(0, 24, 1)) )
fmt = mpld.DateFormatter('%H')
fmt2 = '%Y-%m-%d'
elif nHours < 48:
# < 2 days
Mtick = mpld.HourLocator(byhour=list(range(0, 24, 4)) )
mtick = mpld.HourLocator(byhour=list(range(0, 24, 2)) )
fmt = mpld.DateFormatter('%H')
fmt2 = '%Y-%m-%d'
elif nHours < 72:
# < 3 days
Mtick = mpld.HourLocator(byhour = list(range(0, 24, 6)))
mtick = mpld.HourLocator(byhour = list(range(0, 24, 3)))
fmt = mpld.DateFormatter('%H')
fmt2 = '%Y-%m-%d'
elif nHours < 96:
# < 4 days
Mtick = mpld.HourLocator(byhour = list(range(0, 24, 12)))
mtick = mpld.HourLocator(byhour = list(range(0, 24, 3)))
fmt = mpld.DateFormatter('%H')
fmt2 = '%Y-%m-%d'
elif deltaT.days < 8:
Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1)))
mtick = mpld.HourLocator(byhour=list(range(0, 24, 4)))
fmt = mpld.DateFormatter('%d')
fmt2 = '%Y-%m'
elif deltaT.days < 16:
Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1)))
mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1)))
fmt = mpld.DateFormatter('%d')
fmt2 = '%Y-%m'
elif deltaT.days < 32:
Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 4)))
mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1)))
fmt = mpld.DateFormatter('%d')
fmt2 = '%Y-%m'
elif deltaT.days < 60:
Mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 7)))
mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 1)))
fmt = mpld.DateFormatter('%d')
fmt2 = '%Y-%m'
elif deltaT.days < 183:
Mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1)))
mtick = mpld.DayLocator(bymonthday=list(range(1, 32, 7)))
fmt = mpld.DateFormatter('%m')
fmt2 = '%Y'
elif deltaT.days < 367:
Mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1)))
mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1)))
fmt = mpld.DateFormatter('%m')
fmt2 = '%Y'
elif deltaT.days < 366*2:
Mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 2)))
mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 1)))
fmt = mpld.DateFormatter('%m')
fmt2 = '%Y'
elif deltaT.days < 366*8:
Mtick = mpld.YearLocator(1)
mtick = mpld.MonthLocator(bymonth=list(range(1, 13, 4)))
fmt = mpld.DateFormatter('%Y')
fmt2 = ''
elif deltaT.days < 366*15:
to = axes.lines[0].get_xdata()[0]
tf = axes.lines[0].get_xdata()[-1]
# Ideally would set byyear=list(range(to.year, tf.year,2)) but
# byyear is not a kwarg. Would need to something like
# https://stackoverflow.com/questions/48428729/matplotlib-dates-yearlocator-with-odd-intervals
Mtick = mpld.YearLocator(1)
mtick = mpld.YearLocator(1)
fmt = mpld.DateFormatter('%Y')
fmt2 = ''
if False:
xl = axes.get_xlim()
a = mpld.num2date(xl[0])
print(a)
import pdb;pdb.set_trace()
a = mpld.date2num(a.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0))
b = mpld.num2date(xl[1])
b = mpld.date2num(b.replace(year=(b.year+1), month=1, day=1, hour=0, minute=0, second=0, microsecond=0))
axes.set_xlim([a, b])
elif deltaT.days < 366*40:
Mtick = mpld.YearLocator(5)
mtick = mpld.YearLocator(1)
fmt = mpld.DateFormatter('%Y')
fmt2 = ''
elif deltaT.days < 366*100:
Mtick = mpld.YearLocator(10)
mtick = mpld.YearLocator(2)
fmt = mpld.DateFormatter('%Y')
fmt2 = ''
elif deltaT.days < 366*200:
Mtick = mpld.YearLocator(20)
mtick = mpld.YearLocator(5)
fmt = mpld.DateFormatter('%Y')
fmt2 = ''
else:
Mtick = mpld.YearLocator(50)
mtick = mpld.YearLocator(10)
fmt = mpld.DateFormatter('%Y')
fmt2 = ''
xt = axes.get_xticks()
xl = axes.get_xlim()
if debug:
print("Default xlim[0]: %s" % mpld.num2date(xl[0]))
print("Default xlim[1]: %s" % mpld.num2date(xl[1]))
print("Default xticks[0]: %s" % mpld.num2date(xt[0]))
print("Default xticks[-1]: %s" % mpld.num2date(xt[-1]))
if debug:
print("Start: %s" % mpld.num2date(xl[0]))
print("Stop: %s" % mpld.num2date(xl[1]))
for i in range(0,len(xt)):
print("Tick: %s" % mpld.num2date(xt[i]))
if dir == 'x':
axes.xaxis.set_major_locator(Mtick)
axes.xaxis.set_minor_locator(mtick)
axes.xaxis.set_major_formatter(fmt)
draw(fig) # Render new labels so updated for next line
labels = [item.get_text() for item in axes.get_xticklabels()]
ticks = axes.get_xticks()
time = mpld.num2date(ticks)
else:
axes.yaxis.set_major_locator(Mtick)
axes.yaxis.set_minor_locator(mtick)
axes.yaxis.set_major_formatter(fmt)
draw(fig) # Render new labels so updated for next line
labels = [item.get_text() for item in axes.get_yticklabels()]
ticks = axes.get_yticks()
time = mpld.num2date(ticks)
if debug:
xl = axes.get_xlim()
print(mpld.num2date(xl[0]))
print(mpld.num2date(ticks[0]))
print(mpld.num2date(xl[1]))
print(mpld.num2date(ticks[-1]))
if ticks[0] < xl[0]:
print('Left-most tick label will be clipped.')
if ticks[-1] > xl[1]:
print('Right-most tick label will be clipped.')
for i in range(0,len(ticks)):
print("Tick: %s" % mpld.num2date(ticks[i]))
if fmt2 != '':
first = 0
if ticks[0] < xl[0]:
# Work-around for bug in Matplotlib where left-most tick is less than
# lower x-limit.
first = 1
# Always apply fmt2 to first tick label
labels[first] = '%s\n%s' % (labels[first], datetime.strftime(time[first], fmt2))
for i in range(first+1,len(time)):
# First label will always have fmt applied.
# Modify labels after first under certain conditions.
modify = False
if time[i].year > time[i-1].year:
modify = True
if nDays < 60 and time[i].month > time[i-1].month:
modify = True
if nDays < 4 and time[i].day > time[i-1].day:
modify = True
if nSecs < 60*30 and time[i].hour > time[i-1].hour:
modify = True
if nSecs < 1 and time[i].minute > time[i-1].minute:
modify = True
if nSecs < 1 and time[i].second > time[i-1].second:
modify = True
if not modify: continue
if i == first + 1 and dir == 'x':
# If first two major tick labels have fmt2 applied, the will
# likely run together. This keeps fmt2 label for second major
# tick.
#labels[i] = '%s\n%s' % (labels[i], datetime.strftime(mpld.num2date(ticks[i]), fmt2))
pass
else:
labels[i] = '%s\n%s' % (labels[i], datetime.strftime(mpld.num2date(ticks[i]), fmt2))
if dir == 'x':
axes.set_xticklabels(labels)
if dir == 'y':
axes.set_yticklabels(labels)
# Trigger update of ticks when limits change due to user interaction.
if DOPTS['set_cb']:
if dir == 'x':
axes.callbacks.connect('xlim_changed', on_xlims_change)
else:
axes.callbacks.connect('ylim_changed', on_ylims_change)
def numsize():
'''Returns (width, height) of number '0' in pixels'''
# Not used.
# Based on https://stackoverflow.com/q/5320205
# TODO: numsize(fig, dir) should inspect fig to get used fonts
# for dir='x' and dir='y' and get bounding box for x and y labels.
r = plt.figure().canvas.get_renderer()
t = plt.text(0.5, 0.5, '0')
bb = t.get_window_extent(renderer=r)
w = bb.width
h = bb.height
plt.close()
return (w,h)