Skip to content

Commit 5198ec8

Browse files
committed
Merge remote branch 'brendam/fix-color-kwarg'
* brendam/fix-color-kwarg: remove call to self._get_colors() from inside _maybe_add_color() - not efficient remove comment refactor plotting.py to only have _maybe_add_color defined once fixed color keyword in time series plot - issue #1890
2 parents f391180 + d5e4d0a commit 5198ec8

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

pandas/tests/test_graphics.py

+10
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,16 @@ def test_series_plot_color_kwargs(self):
489489
line = ax.get_lines()[0]
490490
self.assert_(line.get_color() == 'green')
491491

492+
@slow
493+
def test_time_series_plot_color_kwargs(self):
494+
# #1890
495+
import matplotlib.pyplot as plt
496+
497+
plt.close('all')
498+
ax = Series(np.arange(12) + 1, index=date_range('1/1/2000', periods=12)).plot(color='green')
499+
line = ax.get_lines()[0]
500+
self.assert_(line.get_color() == 'green')
501+
492502
PNG_PATH = 'tmp.png'
493503

494504
def _check_plot_works(f, *args, **kwargs):

pandas/tools/plotting.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,12 @@ def _get_colors(self):
876876
colors = self.kwds.pop('colors', cycle)
877877
return has_colors, colors
878878

879+
def _maybe_add_color(self, has_colors, colors, kwds, style, i):
880+
if (not has_colors and
881+
(style is None or re.match('[a-z]+', style) is None)
882+
and 'color' not in kwds):
883+
kwds['color'] = colors[i % len(colors)]
884+
879885
def _make_plot(self):
880886
# this is slightly deceptive
881887
if self.use_index and self._use_dynamic_x():
@@ -886,21 +892,14 @@ def _make_plot(self):
886892
labels = []
887893
x = self._get_xticks(convert_period=True)
888894

889-
has_colors, colors = self._get_colors()
890-
def _maybe_add_color(kwargs, style, i):
891-
if (not has_colors and
892-
(style is None or re.match('[a-z]+', style) is None)
893-
and 'color' not in kwargs):
894-
kwargs['color'] = colors[i % len(colors)]
895-
896895
plotf = self._get_plot_function()
896+
has_colors, colors = self._get_colors()
897897

898898
for i, (label, y) in enumerate(self._iter_data()):
899899
ax = self._get_ax(i)
900900
style = self._get_style(i, label)
901901
kwds = self.kwds.copy()
902-
903-
_maybe_add_color(kwds, style, i)
902+
self._maybe_add_color(has_colors, colors, kwds, style, i)
904903

905904
label = com.pprint_thing(label).encode('utf-8')
906905

@@ -934,11 +933,6 @@ def _make_ts_plot(self, data, **kwargs):
934933
lines = []
935934
labels = []
936935

937-
def _maybe_add_color(kwargs, style, i):
938-
if (not has_colors and
939-
(style is None or re.match('[a-z]+', style) is None)):
940-
kwargs['color'] = colors[i % len(colors)]
941-
942936
def to_leg_label(label, i):
943937
if self.mark_right and self.on_right(i):
944938
return label + ' (right)'
@@ -949,7 +943,7 @@ def to_leg_label(label, i):
949943
style = self.style or ''
950944
label = com.pprint_thing(self.label)
951945
kwds = kwargs.copy()
952-
_maybe_add_color(kwds, style, 0)
946+
self._maybe_add_color(has_colors, colors, kwds, style, 0)
953947

954948
newlines = tsplot(data, plotf, ax=ax, label=label, style=self.style,
955949
**kwds)
@@ -964,7 +958,7 @@ def to_leg_label(label, i):
964958
style = self._get_style(i, col)
965959
kwds = kwargs.copy()
966960

967-
_maybe_add_color(kwds, style, i)
961+
self._maybe_add_color(has_colors, colors, kwds, style, i)
968962

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

0 commit comments

Comments
 (0)