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

Skip to content

Commit 94f2fb8

Browse files
authored
Merge pull request #6686 from Kojoley/move-string_to_bool
MNT: Merged _bool from axis into cbook._string_to_bool
2 parents 068d0b4 + cfd6374 commit 94f2fb8

4 files changed

Lines changed: 19 additions & 26 deletions

File tree

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,8 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
24702470
"""
24712471
if len(kwargs):
24722472
b = True
2473-
b = _string_to_bool(b)
2473+
elif b is not None:
2474+
b = _string_to_bool(b)
24742475

24752476
if axis == 'x' or axis == 'both':
24762477
self.xaxis.grid(b, which=which, **kwargs)

lib/matplotlib/axis.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import matplotlib.artist as artist
1111
from matplotlib.artist import allow_rasterization
1212
import matplotlib.cbook as cbook
13+
from matplotlib.cbook import _string_to_bool
1314
import matplotlib.font_manager as font_manager
1415
import matplotlib.lines as mlines
1516
import matplotlib.patches as mpatches
@@ -802,17 +803,6 @@ def set_tick_params(self, which='major', reset=False, **kw):
802803

803804
@staticmethod
804805
def _translate_tick_kw(kw, to_init_kw=True):
805-
# We may want to move the following function to
806-
# a more visible location; or maybe there already
807-
# is something like this.
808-
def _bool(arg):
809-
if cbook.is_string_like(arg):
810-
if arg.lower() == 'on':
811-
return True
812-
if arg.lower() == 'off':
813-
return False
814-
raise ValueError('String "%s" should be "on" or "off"' % arg)
815-
return bool(arg)
816806
# The following lists may be moved to a more
817807
# accessible location.
818808
kwkeys0 = ['size', 'width', 'color', 'tickdir', 'pad',
@@ -828,22 +818,22 @@ def _bool(arg):
828818
if 'direction' in kw:
829819
kwtrans['tickdir'] = kw.pop('direction')
830820
if 'left' in kw:
831-
kwtrans['tick1On'] = _bool(kw.pop('left'))
821+
kwtrans['tick1On'] = _string_to_bool(kw.pop('left'))
832822
if 'bottom' in kw:
833-
kwtrans['tick1On'] = _bool(kw.pop('bottom'))
823+
kwtrans['tick1On'] = _string_to_bool(kw.pop('bottom'))
834824
if 'right' in kw:
835-
kwtrans['tick2On'] = _bool(kw.pop('right'))
825+
kwtrans['tick2On'] = _string_to_bool(kw.pop('right'))
836826
if 'top' in kw:
837-
kwtrans['tick2On'] = _bool(kw.pop('top'))
827+
kwtrans['tick2On'] = _string_to_bool(kw.pop('top'))
838828

839829
if 'labelleft' in kw:
840-
kwtrans['label1On'] = _bool(kw.pop('labelleft'))
830+
kwtrans['label1On'] = _string_to_bool(kw.pop('labelleft'))
841831
if 'labelbottom' in kw:
842-
kwtrans['label1On'] = _bool(kw.pop('labelbottom'))
832+
kwtrans['label1On'] = _string_to_bool(kw.pop('labelbottom'))
843833
if 'labelright' in kw:
844-
kwtrans['label2On'] = _bool(kw.pop('labelright'))
834+
kwtrans['label2On'] = _string_to_bool(kw.pop('labelright'))
845835
if 'labeltop' in kw:
846-
kwtrans['label2On'] = _bool(kw.pop('labeltop'))
836+
kwtrans['label2On'] = _string_to_bool(kw.pop('labeltop'))
847837
if 'colors' in kw:
848838
c = kw.pop('colors')
849839
kwtrans['color'] = c

lib/matplotlib/cbook.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,15 @@ def is_scalar_or_string(val):
785785

786786

787787
def _string_to_bool(s):
788+
"""Parses the string argument as a boolean"""
788789
if not is_string_like(s):
789-
return s
790-
if s == 'on':
790+
return bool(s)
791+
if s.lower() in ['on', 'true']:
791792
return True
792-
if s == 'off':
793+
if s.lower() in ['off', 'false']:
793794
return False
794-
raise ValueError("string argument must be either 'on' or 'off'")
795+
raise ValueError('String "%s" must be one of: '
796+
'"on", "off", "true", or "false"' % s)
795797

796798

797799
def get_sample_data(fname, asfileobj=True):

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import matplotlib.colorbar
3030
from matplotlib import style
3131
from matplotlib import _pylab_helpers, interactive
32-
from matplotlib.cbook import dedent, silent_list, is_string_like, is_numlike
33-
from matplotlib.cbook import _string_to_bool
32+
from matplotlib.cbook import (dedent, silent_list, is_string_like, is_numlike,
33+
_string_to_bool)
3434
from matplotlib import docstring
3535
from matplotlib.backend_bases import FigureCanvasBase
3636
from matplotlib.figure import Figure, figaspect

0 commit comments

Comments
 (0)