From b8b27463fbfbbb02a84cf74a5c99e296504f5c51 Mon Sep 17 00:00:00 2001 From: Brenda Moon Date: Sun, 7 Oct 2012 17:31:19 +1100 Subject: [PATCH 1/4] fixed color keyword in time series plot - issue #1890 --- pandas/tests/test_graphics.py | 10 ++++++++++ pandas/tools/plotting.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index f2fd98169b585..efb3252a66209 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -489,6 +489,16 @@ def test_series_plot_color_kwargs(self): line = ax.get_lines()[0] self.assert_(line.get_color() == 'green') + @slow + def test_time_series_plot_color_kwargs(self): + # #1890 + import matplotlib.pyplot as plt + + plt.close('all') + ax = Series(np.arange(12) + 1, index=date_range('1/1/2000', periods=12)).plot(color='green') + line = ax.get_lines()[0] + self.assert_(line.get_color() == 'green') + PNG_PATH = 'tmp.png' def _check_plot_works(f, *args, **kwargs): diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 60ed0c70d516b..4a85fb590bc2a 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -936,7 +936,8 @@ def _make_ts_plot(self, data, **kwargs): def _maybe_add_color(kwargs, style, i): if (not has_colors and - (style is None or re.match('[a-z]+', style) is None)): + (style is None or re.match('[a-z]+', style) is None) + and 'color' not in kwargs): kwargs['color'] = colors[i % len(colors)] def to_leg_label(label, i): From 288040778e2c406a321ebb0cc71859c54d916986 Mon Sep 17 00:00:00 2001 From: Brenda Moon Date: Mon, 8 Oct 2012 17:05:19 +1100 Subject: [PATCH 2/4] refactor plotting.py to only have _maybe_add_color defined once --- pandas/tools/plotting.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 4a85fb590bc2a..4195227815abf 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -876,6 +876,13 @@ def _get_colors(self): colors = self.kwds.pop('colors', cycle) return has_colors, colors + def _maybe_add_color(self, kwds, style, i): + has_colors, colors = self._get_colors() + if (not has_colors and + (style is None or re.match('[a-z]+', style) is None) + and 'color' not in kwds): + kwds['color'] = colors[i % len(colors)] + def _make_plot(self): # this is slightly deceptive if self.use_index and self._use_dynamic_x(): @@ -886,13 +893,6 @@ def _make_plot(self): labels = [] x = self._get_xticks(convert_period=True) - has_colors, colors = self._get_colors() - def _maybe_add_color(kwargs, style, i): - if (not has_colors and - (style is None or re.match('[a-z]+', style) is None) - and 'color' not in kwargs): - kwargs['color'] = colors[i % len(colors)] - plotf = self._get_plot_function() for i, (label, y) in enumerate(self._iter_data()): @@ -900,7 +900,8 @@ def _maybe_add_color(kwargs, style, i): style = self._get_style(i, label) kwds = self.kwds.copy() - _maybe_add_color(kwds, style, i) +# not sure if I need to return kwargs & set to kwds or is it pass by reference? + self._maybe_add_color(kwds, style, i) label = _stringify(label) @@ -934,12 +935,6 @@ def _make_ts_plot(self, data, **kwargs): lines = [] labels = [] - def _maybe_add_color(kwargs, style, i): - if (not has_colors and - (style is None or re.match('[a-z]+', style) is None) - and 'color' not in kwargs): - kwargs['color'] = colors[i % len(colors)] - def to_leg_label(label, i): if self.mark_right and self.on_right(i): return label + ' (right)' @@ -950,7 +945,7 @@ def to_leg_label(label, i): style = self.style or '' label = com._stringify(self.label) kwds = kwargs.copy() - _maybe_add_color(kwds, style, 0) + self._maybe_add_color(kwds, style, 0) newlines = tsplot(data, plotf, ax=ax, label=label, style=self.style, **kwds) @@ -965,7 +960,7 @@ def to_leg_label(label, i): style = self._get_style(i, col) kwds = kwargs.copy() - _maybe_add_color(kwds, style, i) + self._maybe_add_color(kwds, style, i) newlines = tsplot(data[col], plotf, ax=ax, label=label, style=style, **kwds) From 75590c444679286fd3b65936ac9670e0149d9587 Mon Sep 17 00:00:00 2001 From: Brenda Moon Date: Mon, 8 Oct 2012 17:15:15 +1100 Subject: [PATCH 3/4] remove comment --- pandas/tools/plotting.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 4195227815abf..6aa0955db21c9 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -899,8 +899,6 @@ def _make_plot(self): ax = self._get_ax(i) style = self._get_style(i, label) kwds = self.kwds.copy() - -# not sure if I need to return kwargs & set to kwds or is it pass by reference? self._maybe_add_color(kwds, style, i) label = _stringify(label) From d5e4d0a59b3390d76c5b4bb608e70e169eb6e3a5 Mon Sep 17 00:00:00 2001 From: Brenda Moon Date: Mon, 8 Oct 2012 18:05:25 +1100 Subject: [PATCH 4/4] remove call to self._get_colors() from inside _maybe_add_color() - not efficient --- pandas/tools/plotting.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 6aa0955db21c9..aa6c23e10c2c6 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -876,8 +876,7 @@ def _get_colors(self): colors = self.kwds.pop('colors', cycle) return has_colors, colors - def _maybe_add_color(self, kwds, style, i): - has_colors, colors = self._get_colors() + def _maybe_add_color(self, has_colors, colors, kwds, style, i): if (not has_colors and (style is None or re.match('[a-z]+', style) is None) and 'color' not in kwds): @@ -894,12 +893,13 @@ def _make_plot(self): x = self._get_xticks(convert_period=True) plotf = self._get_plot_function() + has_colors, colors = self._get_colors() for i, (label, y) in enumerate(self._iter_data()): ax = self._get_ax(i) style = self._get_style(i, label) kwds = self.kwds.copy() - self._maybe_add_color(kwds, style, i) + self._maybe_add_color(has_colors, colors, kwds, style, i) label = _stringify(label) @@ -943,7 +943,7 @@ def to_leg_label(label, i): style = self.style or '' label = com._stringify(self.label) kwds = kwargs.copy() - self._maybe_add_color(kwds, style, 0) + self._maybe_add_color(has_colors, colors, kwds, style, 0) newlines = tsplot(data, plotf, ax=ax, label=label, style=self.style, **kwds) @@ -958,7 +958,7 @@ def to_leg_label(label, i): style = self._get_style(i, col) kwds = kwargs.copy() - self._maybe_add_color(kwds, style, i) + self._maybe_add_color(has_colors, colors, kwds, style, i) newlines = tsplot(data[col], plotf, ax=ax, label=label, style=style, **kwds)