@@ -583,6 +583,12 @@ def drange(dstart, dend, delta):
583
583
f2 = date2num (dinterval_end ) # new float-endpoint
584
584
return np .linspace (f1 , f2 , num + 1 )
585
585
586
+
587
+ def _wrap_in_tex (text ):
588
+ # Braces ensure dashes are not spaced like binary operators.
589
+ return '$\\ mathdefault{' + text .replace ('-' , '{-}' ) + '}$'
590
+
591
+
586
592
## date tickers and formatters ###
587
593
588
594
@@ -597,22 +603,28 @@ class DateFormatter(ticker.Formatter):
597
603
def illegal_s (self ):
598
604
return re .compile (r"((^|[^%])(%%)*%s)" )
599
605
600
- def __init__ (self , fmt , tz = None ):
606
+ def __init__ (self , fmt , tz = None , * , usetex = None ):
601
607
"""
602
608
Parameters
603
609
----------
604
610
fmt : str
605
611
`~datetime.datetime.strftime` format string
606
612
tz : `datetime.tzinfo`, default: :rc:`timezone`
607
613
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.
608
617
"""
609
618
if tz is None :
610
619
tz = _get_rc_timezone ()
611
620
self .fmt = fmt
612
621
self .tz = tz
622
+ self ._usetex = (usetex if usetex is not None else
623
+ mpl .rcParams ['text.usetex' ])
613
624
614
625
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
616
628
617
629
def set_tzinfo (self , tz ):
618
630
self .tz = tz
@@ -685,6 +697,10 @@ class ConciseDateFormatter(ticker.Formatter):
685
697
show_offset : bool, default: True
686
698
Whether to show the offset or not.
687
699
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
+
688
704
Examples
689
705
--------
690
706
See :doc:`/gallery/ticks_and_spines/date_concise_formatter`
@@ -713,7 +729,7 @@ class ConciseDateFormatter(ticker.Formatter):
713
729
"""
714
730
715
731
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 = True ):
717
733
"""
718
734
Autoformat the date labels. The default format is used to form an
719
735
initial string, and then redundant elements are removed.
@@ -768,9 +784,12 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
768
784
'%Y-%b-%d %H:%M' ]
769
785
self .offset_string = ''
770
786
self .show_offset = show_offset
787
+ self ._usetex = (usetex if usetex is not None else
788
+ mpl .rcParams ['text.usetex' ])
771
789
772
790
def __call__ (self , x , pos = None ):
773
- formatter = DateFormatter (self .defaultfmt , self ._tz )
791
+ formatter = DateFormatter (self .defaultfmt , self ._tz ,
792
+ usetex = self ._usetex )
774
793
return formatter (x , pos = pos )
775
794
776
795
def format_ticks (self , values ):
@@ -837,8 +856,13 @@ def format_ticks(self, values):
837
856
if self .show_offset :
838
857
# set the offset string:
839
858
self .offset_string = tickdatetime [- 1 ].strftime (offsetfmts [level ])
859
+ if self ._usetex :
860
+ self .offset_string = _wrap_in_tex (self .offset_string )
840
861
841
- return labels
862
+ if self ._usetex :
863
+ return [_wrap_in_tex (l ) for l in labels ]
864
+ else :
865
+ return labels
842
866
843
867
def get_offset (self ):
844
868
return self .offset_string
@@ -903,7 +927,8 @@ class AutoDateFormatter(ticker.Formatter):
903
927
# Or more simply, perhaps just a format string for each
904
928
# possibility...
905
929
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 ):
907
932
"""
908
933
Autoformat the date labels.
909
934
@@ -918,12 +943,20 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
918
943
defaultfmt : str
919
944
The default format to use if none of the values in ``self.scaled``
920
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.
921
952
"""
922
953
self ._locator = locator
923
954
self ._tz = tz
924
955
self .defaultfmt = defaultfmt
925
956
self ._formatter = DateFormatter (self .defaultfmt , tz )
926
957
rcParams = mpl .rcParams
958
+ self ._usetex = (usetex if usetex is not None else
959
+ mpl .rcParams ['text.usetex' ])
927
960
self .scaled = {
928
961
DAYS_PER_YEAR : rcParams ['date.autoformatter.year' ],
929
962
DAYS_PER_MONTH : rcParams ['date.autoformatter.month' ],
@@ -948,7 +981,7 @@ def __call__(self, x, pos=None):
948
981
self .defaultfmt )
949
982
950
983
if isinstance (fmt , str ):
951
- self ._formatter = DateFormatter (fmt , self ._tz )
984
+ self ._formatter = DateFormatter (fmt , self ._tz , usetex = self . _usetex )
952
985
result = self ._formatter (x , pos )
953
986
elif callable (fmt ):
954
987
result = fmt (x , pos )
0 commit comments