A `.Formatter` which attempts to figure out the best format to use. This is most useful when used with the `AutoDateLocator`. `.AutoDateFormatter` has a ``.scaled`` dictionary that maps tick scales (the interval in days between one major tick) to format strings; this dictionary
| 822 | |
| 823 | |
| 824 | class AutoDateFormatter(ticker.Formatter): |
| 825 | """ |
| 826 | A `.Formatter` which attempts to figure out the best format to use. This |
| 827 | is most useful when used with the `AutoDateLocator`. |
| 828 | |
| 829 | `.AutoDateFormatter` has a ``.scaled`` dictionary that maps tick scales (the |
| 830 | interval in days between one major tick) to format strings; this dictionary |
| 831 | defaults to :: |
| 832 | |
| 833 | self.scaled = { |
| 834 | DAYS_PER_YEAR: rcParams['date.autoformatter.year'], |
| 835 | DAYS_PER_MONTH: rcParams['date.autoformatter.month'], |
| 836 | 1: rcParams['date.autoformatter.day'], |
| 837 | 1 / HOURS_PER_DAY: rcParams['date.autoformatter.hour'], |
| 838 | 1 / MINUTES_PER_DAY: rcParams['date.autoformatter.minute'], |
| 839 | 1 / SEC_PER_DAY: rcParams['date.autoformatter.second'], |
| 840 | 1 / MUSECONDS_PER_DAY: rcParams['date.autoformatter.microsecond'], |
| 841 | } |
| 842 | |
| 843 | The formatter uses the format string corresponding to the lowest key in |
| 844 | the dictionary that is greater or equal to the current scale. Dictionary |
| 845 | entries can be customized:: |
| 846 | |
| 847 | locator = AutoDateLocator() |
| 848 | formatter = AutoDateFormatter(locator) |
| 849 | formatter.scaled[1/(24*60)] = '%M:%S' # only show min and sec |
| 850 | |
| 851 | Custom callables can also be used instead of format strings. The following |
| 852 | example shows how to use a custom format function to strip trailing zeros |
| 853 | from decimal seconds and adds the date to the first ticklabel:: |
| 854 | |
| 855 | def my_format_function(x, pos=None): |
| 856 | x = matplotlib.dates.num2date(x) |
| 857 | if pos == 0: |
| 858 | fmt = '%D %H:%M:%S.%f' |
| 859 | else: |
| 860 | fmt = '%H:%M:%S.%f' |
| 861 | label = x.strftime(fmt) |
| 862 | label = label.rstrip("0") |
| 863 | label = label.rstrip(".") |
| 864 | return label |
| 865 | |
| 866 | formatter.scaled[1/(24*60)] = my_format_function |
| 867 | """ |
| 868 | |
| 869 | # This can be improved by providing some user-level direction on |
| 870 | # how to choose the best format (precedence, etc.). |
| 871 | |
| 872 | # Perhaps a 'struct' that has a field for each time-type where a |
| 873 | # zero would indicate "don't show" and a number would indicate |
| 874 | # "show" with some sort of priority. Same priorities could mean |
| 875 | # show all with the same priority. |
| 876 | |
| 877 | # Or more simply, perhaps just a format string for each |
| 878 | # possibility... |
| 879 | |
| 880 | def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d', *, |
| 881 | usetex=None): |
no outgoing calls
no test coverage detected
searching dependent graphs…