From 78e44a62903fb49c586930fecdec2de16d2a775d Mon Sep 17 00:00:00 2001 From: Nikita Kniazev Date: Mon, 4 Jul 2016 18:39:01 +0300 Subject: [PATCH 1/2] Merged `_bool` from axis into `cbook._string_to_bool` * Modified `_AxesBase.grid` to respect updated behaviour --- lib/matplotlib/axes/_base.py | 3 ++- lib/matplotlib/axis.py | 28 +++++++++------------------- lib/matplotlib/cbook.py | 9 +++++---- lib/matplotlib/pyplot.py | 4 ++-- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f6729dc7e38..6657fd41072 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2470,7 +2470,8 @@ def grid(self, b=None, which='major', axis='both', **kwargs): """ if len(kwargs): b = True - b = _string_to_bool(b) + elif b is not None: + b = _string_to_bool(b) if axis == 'x' or axis == 'both': self.xaxis.grid(b, which=which, **kwargs) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 7f10f0407be..dc3f0bb4332 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -10,6 +10,7 @@ import matplotlib.artist as artist from matplotlib.artist import allow_rasterization import matplotlib.cbook as cbook +from matplotlib.cbook import _string_to_bool import matplotlib.font_manager as font_manager import matplotlib.lines as mlines import matplotlib.patches as mpatches @@ -802,17 +803,6 @@ def set_tick_params(self, which='major', reset=False, **kw): @staticmethod def _translate_tick_kw(kw, to_init_kw=True): - # We may want to move the following function to - # a more visible location; or maybe there already - # is something like this. - def _bool(arg): - if cbook.is_string_like(arg): - if arg.lower() == 'on': - return True - if arg.lower() == 'off': - return False - raise ValueError('String "%s" should be "on" or "off"' % arg) - return bool(arg) # The following lists may be moved to a more # accessible location. kwkeys0 = ['size', 'width', 'color', 'tickdir', 'pad', @@ -828,22 +818,22 @@ def _bool(arg): if 'direction' in kw: kwtrans['tickdir'] = kw.pop('direction') if 'left' in kw: - kwtrans['tick1On'] = _bool(kw.pop('left')) + kwtrans['tick1On'] = _string_to_bool(kw.pop('left')) if 'bottom' in kw: - kwtrans['tick1On'] = _bool(kw.pop('bottom')) + kwtrans['tick1On'] = _string_to_bool(kw.pop('bottom')) if 'right' in kw: - kwtrans['tick2On'] = _bool(kw.pop('right')) + kwtrans['tick2On'] = _string_to_bool(kw.pop('right')) if 'top' in kw: - kwtrans['tick2On'] = _bool(kw.pop('top')) + kwtrans['tick2On'] = _string_to_bool(kw.pop('top')) if 'labelleft' in kw: - kwtrans['label1On'] = _bool(kw.pop('labelleft')) + kwtrans['label1On'] = _string_to_bool(kw.pop('labelleft')) if 'labelbottom' in kw: - kwtrans['label1On'] = _bool(kw.pop('labelbottom')) + kwtrans['label1On'] = _string_to_bool(kw.pop('labelbottom')) if 'labelright' in kw: - kwtrans['label2On'] = _bool(kw.pop('labelright')) + kwtrans['label2On'] = _string_to_bool(kw.pop('labelright')) if 'labeltop' in kw: - kwtrans['label2On'] = _bool(kw.pop('labeltop')) + kwtrans['label2On'] = _string_to_bool(kw.pop('labeltop')) if 'colors' in kw: c = kw.pop('colors') kwtrans['color'] = c diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 5e2de452c50..7f9c6563ae8 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -785,13 +785,14 @@ def is_scalar_or_string(val): def _string_to_bool(s): + """Parses the string argument as a boolean""" if not is_string_like(s): - return s - if s == 'on': + return bool(s) + if s.lower() == 'on': return True - if s == 'off': + if s.lower() == 'off': return False - raise ValueError("string argument must be either 'on' or 'off'") + raise ValueError('String "%s" must be either "on" or "off"' % s) def get_sample_data(fname, asfileobj=True): diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 039382cc4a5..a1a69342ede 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -29,8 +29,8 @@ import matplotlib.colorbar from matplotlib import style from matplotlib import _pylab_helpers, interactive -from matplotlib.cbook import dedent, silent_list, is_string_like, is_numlike -from matplotlib.cbook import _string_to_bool +from matplotlib.cbook import (dedent, silent_list, is_string_like, is_numlike, + _string_to_bool) from matplotlib import docstring from matplotlib.backend_bases import FigureCanvasBase from matplotlib.figure import Figure, figaspect From cfd6374f39d51d824a21b28fdfa60092455bc1c4 Mon Sep 17 00:00:00 2001 From: Nikita Kniazev Date: Mon, 4 Jul 2016 19:56:26 +0300 Subject: [PATCH 2/2] Added support of 'true' and 'false' to `cbook._string_to_bool` --- lib/matplotlib/cbook.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 7f9c6563ae8..f7ba4d95ca4 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -788,11 +788,12 @@ def _string_to_bool(s): """Parses the string argument as a boolean""" if not is_string_like(s): return bool(s) - if s.lower() == 'on': + if s.lower() in ['on', 'true']: return True - if s.lower() == 'off': + if s.lower() in ['off', 'false']: return False - raise ValueError('String "%s" must be either "on" or "off"' % s) + raise ValueError('String "%s" must be one of: ' + '"on", "off", "true", or "false"' % s) def get_sample_data(fname, asfileobj=True):