From 1849626d2047cb4abc2a576fcda792896a0dabe0 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 8 Nov 2015 17:22:45 -0500 Subject: [PATCH 1/4] DFLT: change formats for AutoDateFormatter - Use ISO complient formats by default - aded extra level of scale (seconds) - add rcparams for all of these strings closes #4808 closes #4809 closes #5086 --- doc/users/whats_new/rcparams.rst | 26 +++++++++++++++++-- lib/matplotlib/dates.py | 25 +++++++++--------- .../mpl-data/stylelib/classic.mplstyle | 7 +++++ lib/matplotlib/rcsetup.py | 7 +++++ matplotlibrc.template | 17 ++++++++++++ 5 files changed, 68 insertions(+), 14 deletions(-) diff --git a/doc/users/whats_new/rcparams.rst b/doc/users/whats_new/rcparams.rst index f66080d42b79..82b9d30a5581 100644 --- a/doc/users/whats_new/rcparams.rst +++ b/doc/users/whats_new/rcparams.rst @@ -1,5 +1,27 @@ -Added ``svg.hashsalt`` key to rcParams -``````````````````````````````````````` +Configuration (rcParams) +------------------------ + ++----------------------------+--------------------------------------------------+ +| Parameter | Description | ++============================+==================================================+ +|`date.autoformatter.year` | foramt string for 'year' scale dates | ++----------------------------+--------------------------------------------------+ +|`date.autoformatter.month` | format string for 'month' scale dates | ++----------------------------+--------------------------------------------------+ +|`date.autoformatter.day` | format string for 'day' scale dates | ++----------------------------+--------------------------------------------------+ +|`date.autoformatter.hour` | format string for 'hour' scale times | ++----------------------------+--------------------------------------------------+ +|`date.autoformatter.minute` | format string for 'minute' scale times | ++----------------------------+--------------------------------------------------+ +|`date.autoformatter.second` | format string for 'second' scale times | ++----------------------------+--------------------------------------------------+ +|`svg.hashsalt` | see note | ++----------------------------+--------------------------------------------------+ + +``svg.hashsalt`` +```````````````` + If ``svg.hashsalt`` is ``None`` (which it is by default), the svg backend uses ``uuid4`` to generate the hash salt. If it is not ``None``, it must be a string that is used as the hash salt instead of ``uuid4``. This allows for deterministic SVG output. diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index fb75e2e0bd9f..eb13980a148e 100755 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -114,7 +114,7 @@ from matplotlib.externals import six from matplotlib.externals.six.moves import xrange, zip - +from matplotlib import rcParams import re import time import math @@ -629,12 +629,12 @@ class AutoDateFormatter(ticker.Formatter): format string. The default looks like this:: self.scaled = { - 365.0 : '%Y', - 30. : '%b %Y', - 1.0 : '%b %d %Y', - 1./24. : '%H:%M:%S', - 1. / (24. * 60.): '%H:%M:%S.%f', - } + DAYS_PER_YEAR: rcParams['date.autoformat.year'], + DAYS_PER_MONTH: rcParams['date.autoformat.month'], + 1.0: rcParams['date.autoformat.day'], + 1. / HOURS_PER_DAY: rcParams['date.autoformat.hour'], + 1. / (MINUTES_PER_DAY): rcParams['date.autoformat.minute'], + 1. / (SEC_PER_DAY): rcParams['date.autoformat.second']} The algorithm picks the key in the dictionary that is >= the @@ -685,11 +685,12 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'): self._tz = tz self.defaultfmt = defaultfmt self._formatter = DateFormatter(self.defaultfmt, tz) - self.scaled = {DAYS_PER_YEAR: '%Y', - DAYS_PER_MONTH: '%b %Y', - 1.0: '%b %d %Y', - 1. / HOURS_PER_DAY: '%H:%M:%S', - 1. / (MINUTES_PER_DAY): '%H:%M:%S.%f'} + self.scaled = {DAYS_PER_YEAR: rcParams['date.autoformatter.year'], + DAYS_PER_MONTH: rcParams['date.autoformatter.month'], + 1.0: rcParams['date.autoformatter.day'], + 1. / HOURS_PER_DAY: rcParams['date.autoformatter.hour'], + 1. / (MINUTES_PER_DAY): rcParams['date.autoformatter.minute'], + 1. / (SEC_PER_DAY): rcParams['date.autoformatter.second'],} def __call__(self, x, pos=None): locator_unit_scale = float(self._locator._get_unit()) diff --git a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle index 917b8f202e10..fda80322523b 100644 --- a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle +++ b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle @@ -215,6 +215,13 @@ axes.spines.top : True polaraxes.grid : True # display grid on polar axes axes3d.grid : True # display grid on 3d axes +date.autoformatter.year :'%Y' +date.autoformatter.month :'%b %Y' +date.autoformatter.day :'%b %d %Y' +date.autoformatter.hour :'%H:%M:%S' +date.autoformatter.minute :'%H:%M:%S.%f' +date.autoformatter.second :'%H:%M:%S.%f' + ### TICKS # see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index a52f1199d138..1977a87a1361 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1015,6 +1015,13 @@ def validate_hist_bins(s): 'polaraxes.grid': [True, validate_bool], # display polar grid or # not 'axes3d.grid': [True, validate_bool], # display 3d grid + # TODO validate that these are valid datetime format strings + 'date.autoformatter.year': ['%Y', six.text_type], + 'date.autoformatter.month': ['%Y-%m', six.text_type], + 'date.autoformatter.day': ['%Y-%m-%d', six.text_type], + 'date.autoformatter.hour': ['%H:%M', six.text_type], + 'date.autoformatter.minute': ['%H:%M:%S', six.text_type], + 'date.autoformatter.second': ['%H:%M:%S.%f', six.text_type], #legend properties 'legend.fancybox': [False, validate_bool], diff --git a/matplotlibrc.template b/matplotlibrc.template index 45568b04ef33..f1a15e1b75e4 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -311,6 +311,7 @@ backend : %(backend)s # small compared to the minimum absolute # value of the data. + #axes.unicode_minus : True # use unicode for the minus symbol # rather than hyphen. See # http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes @@ -328,6 +329,22 @@ backend : %(backend)s #polaraxes.grid : True # display grid on polar axes #axes3d.grid : True # display grid on 3d axes +### DATES +# These control the default format strings used in AutoDateFormatter. +# Any valid format datetime format string can be used (see the python +# `datetime` for details). For example using '%%x' will use the locale date representation +# '%%X' will use the locale time representation and '%%c' will use the full locale datetime +# representation. +# These values map to the scales: +# {'year': 365, 'month': 30, 'day': 1, 'hour': 1/24, 'minute': 1 / (24 * 60)} + +# date.autoformatter.year : '%%Y' +# date.autoformatter.month : '%%Y-%%m' +# date.autoformatter.day : '%%Y-%%m-%%d' +# date.autoformatter.hour : '%%H:%%M' +# date.autoformatter.minute : '%%H:%%M:%%S' +# date.autoformatter.second : '%%H:%%M:%%S.%%f' + ### TICKS # see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick From 324529ecd2cace6ddd68eb2b9de6820ca52023f9 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 23 Nov 2015 23:24:09 -0500 Subject: [PATCH 2/4] STY: pep8 --- lib/matplotlib/dates.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index eb13980a148e..dcb029d4293e 100755 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -113,7 +113,7 @@ unicode_literals) from matplotlib.externals import six -from matplotlib.externals.six.moves import xrange, zip +from matplotlib.externals.six.moves import zip from matplotlib import rcParams import re import time @@ -689,8 +689,10 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'): DAYS_PER_MONTH: rcParams['date.autoformatter.month'], 1.0: rcParams['date.autoformatter.day'], 1. / HOURS_PER_DAY: rcParams['date.autoformatter.hour'], - 1. / (MINUTES_PER_DAY): rcParams['date.autoformatter.minute'], - 1. / (SEC_PER_DAY): rcParams['date.autoformatter.second'],} + 1. / (MINUTES_PER_DAY): + rcParams['date.autoformatter.minute'], + 1. / (SEC_PER_DAY): + rcParams['date.autoformatter.second']} def __call__(self, x, pos=None): locator_unit_scale = float(self._locator._get_unit()) From 3a5b577b3944f6c4e8e94b68d02eb261240799e5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 3 Jan 2016 11:54:50 -0500 Subject: [PATCH 3/4] FIX: remove un-needed quotes --- lib/matplotlib/mpl-data/stylelib/classic.mplstyle | 12 ++++++------ matplotlibrc.template | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle index fda80322523b..3777ee9b0c37 100644 --- a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle +++ b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle @@ -215,12 +215,12 @@ axes.spines.top : True polaraxes.grid : True # display grid on polar axes axes3d.grid : True # display grid on 3d axes -date.autoformatter.year :'%Y' -date.autoformatter.month :'%b %Y' -date.autoformatter.day :'%b %d %Y' -date.autoformatter.hour :'%H:%M:%S' -date.autoformatter.minute :'%H:%M:%S.%f' -date.autoformatter.second :'%H:%M:%S.%f' +date.autoformatter.year : %Y +date.autoformatter.month : %b %Y +date.autoformatter.day : %b %d %Y +date.autoformatter.hour : %H:%M:%S +date.autoformatter.minute : %H:%M:%S.%f +date.autoformatter.second : %H:%M:%S.%f ### TICKS # see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick diff --git a/matplotlibrc.template b/matplotlibrc.template index f1a15e1b75e4..8f1db6806e32 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -338,12 +338,12 @@ backend : %(backend)s # These values map to the scales: # {'year': 365, 'month': 30, 'day': 1, 'hour': 1/24, 'minute': 1 / (24 * 60)} -# date.autoformatter.year : '%%Y' -# date.autoformatter.month : '%%Y-%%m' -# date.autoformatter.day : '%%Y-%%m-%%d' -# date.autoformatter.hour : '%%H:%%M' -# date.autoformatter.minute : '%%H:%%M:%%S' -# date.autoformatter.second : '%%H:%%M:%%S.%%f' +# date.autoformatter.year : %%Y +# date.autoformatter.month : %%Y-%%m +# date.autoformatter.day : %%Y-%%m-%%d +# date.autoformatter.hour : %%H:%%M +# date.autoformatter.minute : %%H:%%M:%%S +# date.autoformatter.second : %%H:%%M:%%S.%%f ### TICKS # see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick From 5206cb011e7a43dcaf440139f52dce248611c7fb Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 3 Jan 2016 12:39:32 -0500 Subject: [PATCH 4/4] MNT: use string.Template for matplotlibrc.template Switch to using `string.Template` instead of `%` formatting to generate default matplotlibrc file. This is to avoid having to escape any old or new style format strings that we want to put into the default matplotlibrc file. --- matplotlibrc.template | 14 +++++++------- setup.py | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/matplotlibrc.template b/matplotlibrc.template index 8f1db6806e32..f52ea5794772 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -35,7 +35,7 @@ # You can also deploy your own backend outside of matplotlib by # referring to the module name (which must be in the PYTHONPATH) as # 'module://my_backend'. -backend : %(backend)s +backend : $TEMPLATE_BACKEND # If you are using the Qt4Agg backend, you can choose here # to use the PyQt4 bindings or the newer PySide bindings to @@ -338,12 +338,12 @@ backend : %(backend)s # These values map to the scales: # {'year': 365, 'month': 30, 'day': 1, 'hour': 1/24, 'minute': 1 / (24 * 60)} -# date.autoformatter.year : %%Y -# date.autoformatter.month : %%Y-%%m -# date.autoformatter.day : %%Y-%%m-%%d -# date.autoformatter.hour : %%H:%%M -# date.autoformatter.minute : %%H:%%M:%%S -# date.autoformatter.second : %%H:%%M:%%S.%%f +# date.autoformatter.year : %Y +# date.autoformatter.month : %Y-%m +# date.autoformatter.day : %Y-%m-%d +# date.autoformatter.hour : %H:%M +# date.autoformatter.minute : %H:%M:%S +# date.autoformatter.second : %H:%M:%S.%f ### TICKS # see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick diff --git a/setup.py b/setup.py index 07e431ca7364..db00a6ed2241 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ """ from __future__ import print_function, absolute_import - +from string import Template # This needs to be the very first thing to use distribute from distribute_setup import use_setuptools use_setuptools() @@ -230,8 +230,9 @@ def run(self): default_backend = setupext.options['backend'] with open('matplotlibrc.template') as fd: template = fd.read() + template = Template(template) with open('lib/matplotlib/mpl-data/matplotlibrc', 'w') as fd: - fd.write(template % {'backend': default_backend}) + fd.write(template.safe_substitute(TEMPLATE_BACKEND=default_backend)) # Build in verbose mode if requested if setupext.options['verbose']: