162162import dateutil .tz
163163import numpy as np
164164
165- import matplotlib
165+ import matplotlib as mpl
166166import matplotlib .units as units
167167import matplotlib .cbook as cbook
168168import matplotlib .ticker as ticker
187187
188188def _get_rc_timezone ():
189189 """Retrieve the preferred timezone from the rcParams dictionary."""
190- s = matplotlib .rcParams ['timezone' ]
190+ s = mpl .rcParams ['timezone' ]
191191 if s == 'UTC' :
192192 return UTC
193193 return dateutil .tz .gettz (s )
@@ -281,7 +281,7 @@ def get_epoch():
281281 global _epoch
282282
283283 if _epoch is None :
284- _epoch = matplotlib .rcParams ['date.epoch' ]
284+ _epoch = mpl .rcParams ['date.epoch' ]
285285 return _epoch
286286
287287
@@ -583,6 +583,12 @@ def drange(dstart, dend, delta):
583583 f2 = date2num (dinterval_end ) # new float-endpoint
584584 return np .linspace (f1 , f2 , num + 1 )
585585
586+
587+ def _wrap_in_tex (text ):
588+ # Braces ensure dashes are not spaced like binary operators.
589+ return '$\\ mathdefault{' + text .replace ('-' , '{-}' ) + '}$'
590+
591+
586592## date tickers and formatters ###
587593
588594
@@ -597,22 +603,28 @@ class DateFormatter(ticker.Formatter):
597603 def illegal_s (self ):
598604 return re .compile (r"((^|[^%])(%%)*%s)" )
599605
600- def __init__ (self , fmt , tz = None ):
606+ def __init__ (self , fmt , tz = None , * , usetex = None ):
601607 """
602608 Parameters
603609 ----------
604610 fmt : str
605611 `~datetime.datetime.strftime` format string
606612 tz : `datetime.tzinfo`, default: :rc:`timezone`
607613 Ticks timezone.
614+ usetex : bool, default: :rc:`text.usetex`
615+ To enable/disable the use of TeX's math mode for rendering the
616+ results of the formatter.
608617 """
609618 if tz is None :
610619 tz = _get_rc_timezone ()
611620 self .fmt = fmt
612621 self .tz = tz
622+ self ._usetex = (usetex if usetex is not None else
623+ mpl .rcParams ['text.usetex' ])
613624
614625 def __call__ (self , x , pos = 0 ):
615- return num2date (x , self .tz ).strftime (self .fmt )
626+ result = num2date (x , self .tz ).strftime (self .fmt )
627+ return _wrap_in_tex (result ) if self ._usetex else result
616628
617629 def set_tzinfo (self , tz ):
618630 self .tz = tz
@@ -685,6 +697,10 @@ class ConciseDateFormatter(ticker.Formatter):
685697 show_offset : bool, default: True
686698 Whether to show the offset or not.
687699
700+ usetex : bool, default: :rc:`text.usetex`
701+ To enable/disable the use of TeX's math mode for rendering the results
702+ of the formatter.
703+
688704 Examples
689705 --------
690706 See :doc:`/gallery/ticks_and_spines/date_concise_formatter`
@@ -713,7 +729,7 @@ class ConciseDateFormatter(ticker.Formatter):
713729 """
714730
715731 def __init__ (self , locator , tz = None , formats = None , offset_formats = None ,
716- zero_formats = None , show_offset = True ):
732+ zero_formats = None , show_offset = True , * , usetex = None ):
717733 """
718734 Autoformat the date labels. The default format is used to form an
719735 initial string, and then redundant elements are removed.
@@ -768,9 +784,12 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
768784 '%Y-%b-%d %H:%M' ]
769785 self .offset_string = ''
770786 self .show_offset = show_offset
787+ self ._usetex = (usetex if usetex is not None else
788+ mpl .rcParams ['text.usetex' ])
771789
772790 def __call__ (self , x , pos = None ):
773- formatter = DateFormatter (self .defaultfmt , self ._tz )
791+ formatter = DateFormatter (self .defaultfmt , self ._tz ,
792+ usetex = self ._usetex )
774793 return formatter (x , pos = pos )
775794
776795 def format_ticks (self , values ):
@@ -837,8 +856,13 @@ def format_ticks(self, values):
837856 if self .show_offset :
838857 # set the offset string:
839858 self .offset_string = tickdatetime [- 1 ].strftime (offsetfmts [level ])
859+ if self ._usetex :
860+ self .offset_string = _wrap_in_tex (self .offset_string )
840861
841- return labels
862+ if self ._usetex :
863+ return [_wrap_in_tex (l ) for l in labels ]
864+ else :
865+ return labels
842866
843867 def get_offset (self ):
844868 return self .offset_string
@@ -903,17 +927,36 @@ class AutoDateFormatter(ticker.Formatter):
903927 # Or more simply, perhaps just a format string for each
904928 # possibility...
905929
906- def __init__ (self , locator , tz = None , defaultfmt = '%Y-%m-%d' ):
930+ def __init__ (self , locator , tz = None , defaultfmt = '%Y-%m-%d' , * ,
931+ usetex = None ):
907932 """
908- Autoformat the date labels. The default format is the one to use
909- if none of the values in ``self.scaled`` are greater than the unit
910- returned by ``locator._get_unit()``.
933+ Autoformat the date labels.
934+
935+ Parameters
936+ ----------
937+ locator : `.ticker.Locator`
938+ Locator that this axis is using.
939+
940+ tz : str, optional
941+ Passed to `.dates.date2num`.
942+
943+ defaultfmt : str
944+ The default format to use if none of the values in ``self.scaled``
945+ are greater than the unit returned by ``locator._get_unit()``.
946+
947+ usetex : bool, default: :rc:`text.usetex`
948+ To enable/disable the use of TeX's math mode for rendering the
949+ results of the formatter. If any entries in ``self.scaled`` are set
950+ as functions, then it is up to the customized function to enable or
951+ disable TeX's math mode itself.
911952 """
912953 self ._locator = locator
913954 self ._tz = tz
914955 self .defaultfmt = defaultfmt
915956 self ._formatter = DateFormatter (self .defaultfmt , tz )
916- rcParams = matplotlib .rcParams
957+ rcParams = mpl .rcParams
958+ self ._usetex = (usetex if usetex is not None else
959+ mpl .rcParams ['text.usetex' ])
917960 self .scaled = {
918961 DAYS_PER_YEAR : rcParams ['date.autoformatter.year' ],
919962 DAYS_PER_MONTH : rcParams ['date.autoformatter.month' ],
@@ -938,7 +981,7 @@ def __call__(self, x, pos=None):
938981 self .defaultfmt )
939982
940983 if isinstance (fmt , str ):
941- self ._formatter = DateFormatter (fmt , self ._tz )
984+ self ._formatter = DateFormatter (fmt , self ._tz , usetex = self . _usetex )
942985 result = self ._formatter (x , pos )
943986 elif callable (fmt ):
944987 result = fmt (x , pos )
0 commit comments