Skip to content

fixed color keyword in time series plot - issue #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 31, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 10 additions & 16 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,12 @@ def _get_colors(self):
colors = self.kwds.pop('colors', cycle)
return has_colors, 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):
kwds['color'] = colors[i % len(colors)]

def _make_plot(self):
# this is slightly deceptive
if self.use_index and self._use_dynamic_x():
Expand All @@ -886,21 +892,14 @@ 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()
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()

_maybe_add_color(kwds, style, i)
self._maybe_add_color(has_colors, colors, kwds, style, i)

label = _stringify(label)

Expand Down Expand Up @@ -934,11 +933,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)):
kwargs['color'] = colors[i % len(colors)]

def to_leg_label(label, i):
if self.mark_right and self.on_right(i):
return label + ' (right)'
Expand All @@ -949,7 +943,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(has_colors, colors, kwds, style, 0)

newlines = tsplot(data, plotf, ax=ax, label=label, style=self.style,
**kwds)
Expand All @@ -964,7 +958,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(has_colors, colors, kwds, style, i)

newlines = tsplot(data[col], plotf, ax=ax, label=label,
style=style, **kwds)
Expand Down