@@ -517,9 +517,25 @@ class AutoDateFormatter(ticker.Formatter):
517
517
dictionary by doing::
518
518
519
519
520
- formatter = AutoDateFormatter()
521
- formatter.scaled[1/(24.*60.)] = '%M:%S' # only show min and sec
522
-
520
+ >>> formatter = AutoDateFormatter()
521
+ >>> formatter.scaled[1/(24.*60.)] = '%M:%S' # only show min and sec
522
+
523
+ Custom `FunctionFormatter`s can also be used. The following example shows
524
+ how to use a custom format function to strip trailing zeros from decimal
525
+ seconds and adds the date to the first ticklabel::
526
+
527
+ >>> def my_format_function(x, pos=None):
528
+ ... x = matplotlib.dates.num2date(x)
529
+ ... if pos == 0:
530
+ ... fmt = '%D %H:%M:%S.%f'
531
+ ... else:
532
+ ... fmt = '%H:%M:%S.%f'
533
+ ... label = x.strftime(fmt)
534
+ ... label = label.rstrip("0")
535
+ ... label = label.rstrip(".")
536
+ ... return label
537
+ >>> from matplotlib.ticker import FuncFormatter
538
+ >>> formatter.scaled[1/(24.*60.)] = FuncFormatter(my_format_function)
523
539
"""
524
540
525
541
# This can be improved by providing some user-level direction on
@@ -535,8 +551,9 @@ class AutoDateFormatter(ticker.Formatter):
535
551
536
552
def __init__ (self , locator , tz = None , defaultfmt = '%Y-%m-%d' ):
537
553
"""
538
- Autofmt the date labels. The default format is the one to use
539
- if none of the times in scaled match
554
+ Autoformat the date labels. The default format is the one to use
555
+ if none of the values in ``self.scaled`` are greater than the unit
556
+ returned by ``locator._get_unit()``.
540
557
"""
541
558
self ._locator = locator
542
559
self ._tz = tz
@@ -548,17 +565,25 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
548
565
1. / 24. : '%H:%M:%S' ,
549
566
1. / (24. * 60. ): '%H:%M:%S.%f' }
550
567
551
- def __call__ (self , x , pos = 0 ):
552
- scale = float (self ._locator ._get_unit ())
568
+ def __call__ (self , x , pos = None ):
569
+ locator_unit_scale = float (self ._locator ._get_unit ())
553
570
fmt = self .defaultfmt
554
571
555
- for k in sorted (self .scaled ):
556
- if k >= scale :
557
- fmt = self .scaled [k ]
572
+ # Pick the first scale which is greater than the locator unit.
573
+ for possible_scale in sorted (self .scaled ):
574
+ if possible_scale >= locator_unit_scale :
575
+ fmt = self .scaled [possible_scale ]
558
576
break
559
577
560
- self ._formatter = DateFormatter (fmt , self ._tz )
561
- return self ._formatter (x , pos )
578
+ if isinstance (fmt , six .string_types ):
579
+ self ._formatter = DateFormatter (fmt , self ._tz )
580
+ result = self ._formatter (x , pos )
581
+ elif six .callable (fmt ):
582
+ result = fmt (x , pos )
583
+ else :
584
+ raise TypeError ('Unexpected type passed to {!r}.' .formatter (self ))
585
+
586
+ return result
562
587
563
588
564
589
class rrulewrapper :
0 commit comments