From 5a7dab7b578036afc65e30aa0a6ab8259cbc69b6 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 3 May 2016 20:09:02 -0400 Subject: [PATCH 1/2] API: bar now color cycles repeated calls to `ax.bar` will advance the patch color cycle. closes #5854 --- lib/matplotlib/axes/_axes.py | 15 +++++++-------- lib/matplotlib/tests/test_axes.py | 13 ++++++++++++- lib/matplotlib/tests/test_bbox_tight.py | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ce852d75cb8a..4b862f0f377e 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1983,6 +1983,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs): if not self._hold: self.cla() color = kwargs.pop('color', None) + if color is None: + color = self._get_patches_for_fill.get_next_color() edgecolor = kwargs.pop('edgecolor', None) linewidth = kwargs.pop('linewidth', None) @@ -2062,14 +2064,11 @@ def make_iterable(x): if len(linewidth) < nbars: linewidth *= nbars - if color is None: - color = [None] * nbars - else: - color = list(mcolors.to_rgba_array(color)) - if len(color) == 0: # until to_rgba_array is changed - color = [[0, 0, 0, 0]] - if len(color) < nbars: - color *= nbars + color = list(mcolors.to_rgba_array(color)) + if len(color) == 0: # until to_rgba_array is changed + color = [[0, 0, 0, 0]] + if len(color) < nbars: + color *= nbars if edgecolor is None: edgecolor = [None] * nbars diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index e4dbc660683f..4b5b8fd882d6 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -29,7 +29,7 @@ from numpy.testing import assert_allclose, assert_array_equal import warnings from matplotlib.cbook import IgnoredKeywordWarning - +import matplotlib.colors as mcolors # Note: Some test cases are run twice: once normally and once with labeled data # These two must be defined in the same test function or need to have @@ -4492,6 +4492,17 @@ def test_large_offset(): fig.canvas.draw() +def test_bar_color_cycle(): + ccov = mcolors.colorConverter.to_rgb + fig, ax = plt.subplots() + for j in range(5): + ln, = ax.plot(range(3)) + brs = ax.bar(range(3), range(3)) + for br in brs: + print(ln.get_color(), br.get_facecolor()) + assert ccov(ln.get_color()) == ccov(br.get_facecolor()) + + if __name__ == '__main__': import nose import sys diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index 7e05e5fd5432..76e2e3427393 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -34,7 +34,7 @@ def test_bbox_inches_tight(): # the bottom values for stacked bar chart fig, ax = plt.subplots(1, 1) for row in xrange(rows): - plt.bar(ind, data[row], width, bottom=yoff) + ax.bar(ind, data[row], width, bottom=yoff, color='b') yoff = yoff + data[row] cellText.append(['']) plt.xticks([]) From 5e556808f1ba76a68d710a67eefd49c627e93d62 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sun, 22 May 2016 14:44:34 -0400 Subject: [PATCH 2/2] MNT: add kwarg normalization to ax.bar --- lib/matplotlib/axes/_axes.py | 1 + lib/matplotlib/patches.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 4b862f0f377e..4189e6e8724a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1980,6 +1980,7 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs): .. plot:: mpl_examples/pylab_examples/bar_stacked.py """ + kwargs = cbook.normalize_kwargs(kwargs, mpatches._patch_alias_map) if not self._hold: self.cla() color = kwargs.pop('color', None) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index fa22b681976a..6722003751d0 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -55,6 +55,14 @@ """) +_patch_alias_map = { + 'antialiased': ['aa'], + 'edgecolor': ['ec'], + 'facecolor': ['fc'], + 'linewidth': ['lw'], + 'linestyle': ['ls'] + } + class Patch(artist.Artist): """