From ee2e1c9e2d0c5ee5cd9686457da3f6cc078ad2fe Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 13 Oct 2015 13:34:32 -0400 Subject: [PATCH 1/3] MNT: clean up imports a bit --- lib/matplotlib/axes/_axes.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 97e5e55d90ac..5114ecc5362c 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -14,7 +14,8 @@ from matplotlib import unpack_labeled_data import matplotlib.cbook as cbook -from matplotlib.cbook import mplDeprecation, STEP_LOOKUP_MAP +from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP, is_sequence_of_strings, + iterable, is_string_like) import matplotlib.collections as mcoll import matplotlib.colors as mcolors import matplotlib.contour as mcontour @@ -43,10 +44,6 @@ rcParams = matplotlib.rcParams -iterable = cbook.iterable -is_string_like = cbook.is_string_like -is_sequence_of_strings = cbook.is_sequence_of_strings - def _plot_args_replacer(args, data): if len(args) == 1: From 5873ed1c3134438db168dc71c3a98a939eeb3f89 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 13 Oct 2015 13:37:19 -0400 Subject: [PATCH 2/3] MNT: consolidate tick_label logic Collect (almost) all of the logic for the bar tick_label in one place. Closes #5237 --- lib/matplotlib/axes/_axes.py | 23 ++++++++++++----------- lib/matplotlib/tests/test_axes.py | 7 +++++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5114ecc5362c..198109116db7 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1980,9 +1980,6 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs): edgecolor = kwargs.pop('edgecolor', None) linewidth = kwargs.pop('linewidth', None) - tick_label = kwargs.pop('tick_label', None) - label_ticks_flag = tick_label is not None - # Because xerr and yerr will be passed to errorbar, # most dimension checking and processing will be left # to the errorbar method. @@ -1998,6 +1995,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs): orientation = kwargs.pop('orientation', 'vertical') log = kwargs.pop('log', False) label = kwargs.pop('label', '') + tick_labels = kwargs.pop('tick_label', None) def make_iterable(x): if not iterable(x): @@ -2013,7 +2011,6 @@ def make_iterable(x): _bottom = bottom bottom = make_iterable(bottom) linewidth = make_iterable(linewidth) - tick_label = make_iterable(tick_label) adjust_ylim = False adjust_xlim = False @@ -2058,8 +2055,6 @@ def make_iterable(x): if len(linewidth) < nbars: linewidth *= nbars - if len(tick_label) < nbars: - tick_label *= nbars if color is None: color = [None] * nbars @@ -2092,9 +2087,6 @@ def make_iterable(x): if len(bottom) != nbars: raise ValueError("incompatible sizes: argument 'bottom' " "must be length %d or scalar" % nbars) - if len(tick_label) != nbars: - raise ValueError("incompatible sizes: argument 'tick_label' " - "must be length %d or string" % nbars) patches = [] @@ -2190,9 +2182,18 @@ def make_iterable(x): bar_container = BarContainer(patches, errorbar, label=label) self.add_container(bar_container) - if label_ticks_flag: + if tick_labels is not None: + tick_labels = make_iterable(tick_labels) + if not is_sequence_of_strings(tick_labels): + raise ValueError("tick_label must be a sequence of strings") + if len(tick_labels) == 1: + tick_labels *= tick_label_position + if len(tick_labels) != tick_label_position: + raise ValueError("incompatible sizes: argument 'tick_label' " + "must be length %d or string" % nbars) + tick_label_axis.set_ticks(tick_label_position) - tick_label_axis.set_ticklabels(tick_label) + tick_label_axis.set_ticklabels(tick_labels) return bar_container diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f254e17b7449..e985a015bed2 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1055,6 +1055,12 @@ def test_bar_tick_label_single(): ax.bar("a", "b" , tick_label='a', data=data) +@cleanup +def test_bar_ticklabel_fail(): + fig, ax = plt.subplots() + ax.bar([], []) + + @image_comparison(baseline_images=['bar_tick_label_multiple'], extensions=['png']) def test_bar_tick_label_multiple(): @@ -1082,6 +1088,7 @@ def test_hist_log(): ax = fig.add_subplot(111) ax.hist(data, fill=False, log=True) + @image_comparison(baseline_images=['hist_bar_empty'], remove_text=True, extensions=['png']) def test_hist_bar_empty(): From cbad5d715ccdf59e4f1d84647daf41e1f885a386 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 13 Oct 2015 14:36:22 -0400 Subject: [PATCH 3/3] FIX: fix all of the weird input validation --- lib/matplotlib/axes/_axes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 198109116db7..4d7a9c9a45c3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -14,7 +14,7 @@ from matplotlib import unpack_labeled_data import matplotlib.cbook as cbook -from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP, is_sequence_of_strings, +from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP, iterable, is_string_like) import matplotlib.collections as mcoll import matplotlib.colors as mcolors @@ -2184,11 +2184,11 @@ def make_iterable(x): if tick_labels is not None: tick_labels = make_iterable(tick_labels) - if not is_sequence_of_strings(tick_labels): - raise ValueError("tick_label must be a sequence of strings") + if isinstance(tick_labels, six.string_types): + tick_labels = [tick_labels] if len(tick_labels) == 1: - tick_labels *= tick_label_position - if len(tick_labels) != tick_label_position: + tick_labels *= nbars + if len(tick_labels) != nbars: raise ValueError("incompatible sizes: argument 'tick_label' " "must be length %d or string" % nbars)