Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a4c7ef

Browse files
authored
Merge pull request #19733 from anntzer/datesdoc
Reword AutoDateFormatter docs.
2 parents 3b2c375 + 7b0b9b6 commit 6a4c7ef

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

lib/matplotlib/dates.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def format_ticks(self, values):
812812
# year, month, day etc.
813813
# fmt for most ticks at this level
814814
fmts = self.formats
815-
# format beginnings of days, months, years, etc...
815+
# format beginnings of days, months, years, etc.
816816
zerofmts = self.zero_formats
817817
# offset fmt are for the offset in the upper left of the
818818
# or lower right of the axis.
@@ -888,48 +888,48 @@ class AutoDateFormatter(ticker.Formatter):
888888
A `.Formatter` which attempts to figure out the best format to use. This
889889
is most useful when used with the `AutoDateLocator`.
890890
891-
The AutoDateFormatter has a scale dictionary that maps the scale
892-
of the tick (the distance in days between one major tick) and a
893-
format string. The default looks like this::
891+
`.AutoDateFormatter` has a ``.scale`` dictionary that maps tick scales (the
892+
interval in days between one major tick) to format strings; this dictionary
893+
defaults to ::
894894
895895
self.scaled = {
896896
DAYS_PER_YEAR: rcParams['date.autoformat.year'],
897897
DAYS_PER_MONTH: rcParams['date.autoformat.month'],
898-
1.0: rcParams['date.autoformat.day'],
899-
1. / HOURS_PER_DAY: rcParams['date.autoformat.hour'],
900-
1. / (MINUTES_PER_DAY): rcParams['date.autoformat.minute'],
901-
1. / (SEC_PER_DAY): rcParams['date.autoformat.second'],
902-
1. / (MUSECONDS_PER_DAY): rcParams['date.autoformat.microsecond'],
898+
1: rcParams['date.autoformat.day'],
899+
1 / HOURS_PER_DAY: rcParams['date.autoformat.hour'],
900+
1 / MINUTES_PER_DAY: rcParams['date.autoformat.minute'],
901+
1 / SEC_PER_DAY: rcParams['date.autoformat.second'],
902+
1 / MUSECONDS_PER_DAY: rcParams['date.autoformat.microsecond'],
903903
}
904904
905-
The algorithm picks the key in the dictionary that is >= the
906-
current scale and uses that format string. You can customize this
907-
dictionary by doing::
905+
The formatter uses the format string corresponding to the lowest key in
906+
the dictionary that is greater or equal to the current scale. Dictionary
907+
entries can be customized::
908908
909-
>>> locator = AutoDateLocator()
910-
>>> formatter = AutoDateFormatter(locator)
911-
>>> formatter.scaled[1/(24.*60.)] = '%M:%S' # only show min and sec
912-
913-
A custom `.FuncFormatter` can also be used. The following example shows
914-
how to use a custom format function to strip trailing zeros from decimal
915-
seconds and adds the date to the first ticklabel::
916-
917-
>>> def my_format_function(x, pos=None):
918-
... x = matplotlib.dates.num2date(x)
919-
... if pos == 0:
920-
... fmt = '%D %H:%M:%S.%f'
921-
... else:
922-
... fmt = '%H:%M:%S.%f'
923-
... label = x.strftime(fmt)
924-
... label = label.rstrip("0")
925-
... label = label.rstrip(".")
926-
... return label
927-
>>> from matplotlib.ticker import FuncFormatter
928-
>>> formatter.scaled[1/(24.*60.)] = FuncFormatter(my_format_function)
909+
locator = AutoDateLocator()
910+
formatter = AutoDateFormatter(locator)
911+
formatter.scaled[1/(24*60)] = '%M:%S' # only show min and sec
912+
913+
Custom callables can also be used instead of format strings. The following
914+
example shows how to use a custom format function to strip trailing zeros
915+
from decimal seconds and adds the date to the first ticklabel::
916+
917+
def my_format_function(x, pos=None):
918+
x = matplotlib.dates.num2date(x)
919+
if pos == 0:
920+
fmt = '%D %H:%M:%S.%f'
921+
else:
922+
fmt = '%H:%M:%S.%f'
923+
label = x.strftime(fmt)
924+
label = label.rstrip("0")
925+
label = label.rstrip(".")
926+
return label
927+
928+
formatter.scaled[1/(24*60)] = my_format_function
929929
"""
930930

931931
# This can be improved by providing some user-level direction on
932-
# how to choose the best format (precedence, etc...)
932+
# how to choose the best format (precedence, etc.).
933933

934934
# Perhaps a 'struct' that has a field for each time-type where a
935935
# zero would indicate "don't show" and a number would indicate

0 commit comments

Comments
 (0)