From bbfcbd136748600d48ad2d385a5906439ac89863 Mon Sep 17 00:00:00 2001 From: Randy Carnevale Date: Fri, 18 Dec 2015 15:15:36 -0500 Subject: [PATCH] look for colormap in rcParams['axes.prop_cycle'] (mpl 1.5+) first --- doc/source/whatsnew/v0.18.0.txt | 3 +-- pandas/tests/test_graphics.py | 19 ++++++++++++++++++- pandas/tools/plotting.py | 7 +++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 5b8c282d3e4ed..cd404476e1d7e 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -227,8 +227,7 @@ Bug Fixes - Bug in parsing timezone offset strings with non-zero minutes (:issue:`11708`) - - +- Bug in ``df.plot`` using incorrect colors for bar plots under matplotlib 1.5+ (:issue:`11614`) - Bug in ``.loc`` result with duplicated key may have ``Index`` with incorrect dtype (:issue:`11497`) - Bug in ``pd.rolling_median`` where memory allocation failed even with sufficient memory (:issue:`11696`) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index acf6f4b0bd48c..27ca4c157f594 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -3716,6 +3716,24 @@ def test_plain_axes(self): Series(rand(10)).plot(ax=ax) Series(rand(10)).plot(ax=iax) + def test_passed_bar_colors(self): + import matplotlib as mpl + color_tuples = [(0.9, 0, 0, 1), (0, 0.9, 0, 1), (0, 0, 0.9, 1)] + colormap = mpl.colors.ListedColormap(color_tuples) + barplot = pd.DataFrame([[1,2,3]]).plot(kind="bar", cmap=colormap) + self.assertEqual(color_tuples, [c.get_facecolor() for c in barplot.patches]) + + def test_rcParams_bar_colors(self): + import matplotlib as mpl + color_tuples = [(0.9, 0, 0, 1), (0, 0.9, 0, 1), (0, 0, 0.9, 1)] + try: # mpl 1.5 + with mpl.rc_context(rc={'axes.prop_cycle': mpl.cycler("color", color_tuples)}): + barplot = pd.DataFrame([[1,2,3]]).plot(kind="bar") + except (AttributeError, KeyError): # mpl 1.4 + with mpl.rc_context(rc={'axes.color_cycle': color_tuples}): + barplot = pd.DataFrame([[1,2,3]]).plot(kind="bar") + self.assertEqual(color_tuples, [c.get_facecolor() for c in barplot.patches]) + @tm.mplskip class TestDataFrameGroupByPlots(TestPlotBase): @@ -3763,7 +3781,6 @@ def test_plot_submethod_works(self): df.groupby('z')['x'].plot.line() tm.close() - def assert_is_valid_plot_return_object(objs): import matplotlib.pyplot as plt if isinstance(objs, np.ndarray): diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 24b37ce39631b..3e9c788914a5a 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -163,8 +163,11 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default', if color_type == 'default': # need to call list() on the result to copy so we don't # modify the global rcParams below - colors = list(plt.rcParams.get('axes.color_cycle', - list('bgrcmyk'))) + try: + colors = [c['color'] for c in list(plt.rcParams['axes.prop_cycle'])] + except KeyError: + colors = list(plt.rcParams.get('axes.color_cycle', + list('bgrcmyk'))) if isinstance(colors, compat.string_types): colors = list(colors) elif color_type == 'random':