Skip to content

Commit 98b5509

Browse files
committed
BUG: color cycle problems
1 parent eae588d commit 98b5509

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

pandas/tools/plotting.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -869,23 +869,26 @@ def _use_dynamic_x(self):
869869

870870
return (freq is not None) and self._is_dynamic_freq(freq)
871871

872+
def _get_colors(self):
873+
import matplotlib.pyplot as plt
874+
cycle = ''.join(plt.rcParams.get('axes.color_cycle', list('bgrcmyk')))
875+
has_colors = 'colors' in self.kwds
876+
colors = self.kwds.pop('colors', cycle)
877+
return has_colors, colors
878+
872879
def _make_plot(self):
873880
# this is slightly deceptive
874881
if self.use_index and self._use_dynamic_x():
875882
data = self._maybe_convert_index(self.data)
876883
self._make_ts_plot(data, **self.kwds)
877884
else:
878-
import matplotlib.pyplot as plt
879-
cycle = ''.join(plt.rcParams.get('axes.color_cycle',
880-
list('bgrcmyk')))
881-
has_colors = 'colors' in self.kwds
882-
colors = self.kwds.pop('colors', cycle)
883885
lines = []
884886
labels = []
885887
x = self._get_xticks(convert_period=True)
886888

889+
has_colors, colors = self._get_colors()
887890
def _maybe_add_color(kwargs, style, i):
888-
if (has_colors and
891+
if (not has_colors and
889892
(style is None or re.match('[a-z]+', style) is None)):
890893
kwargs['color'] = colors[i % len(colors)]
891894

@@ -923,19 +926,15 @@ def _maybe_add_color(kwargs, style, i):
923926

924927
def _make_ts_plot(self, data, **kwargs):
925928
from pandas.tseries.plotting import tsplot
926-
import matplotlib.pyplot as plt
927929
kwargs = kwargs.copy()
928-
cycle = ''.join(plt.rcParams.get('axes.color_cycle', list('bgrcmyk')))
929-
930-
has_colors = 'colors' in kwargs
931-
colors = kwargs.pop('colors', ''.join(cycle))
930+
has_colors, colors = self._get_colors()
932931

933932
plotf = self._get_plot_function()
934933
lines = []
935934
labels = []
936935

937936
def _maybe_add_color(kwargs, style, i):
938-
if (has_colors and
937+
if (not has_colors and
939938
(style is None or re.match('[a-z]+', style) is None)):
940939
kwargs['color'] = colors[i % len(colors)]
941940

@@ -948,11 +947,11 @@ def to_leg_label(label, i):
948947
ax = self._get_ax(0) #self.axes[0]
949948
style = self.style or ''
950949
label = com._stringify(self.label)
951-
952-
_maybe_add_color(kwargs, style, 0)
950+
kwds = kwargs.copy()
951+
_maybe_add_color(kwds, style, 0)
953952

954953
newlines = tsplot(data, plotf, ax=ax, label=label, style=self.style,
955-
**kwargs)
954+
**kwds)
956955
ax.grid(self.grid)
957956
lines.append(newlines[0])
958957
leg_label = to_leg_label(label, 0)
@@ -964,7 +963,7 @@ def to_leg_label(label, i):
964963
style = self._get_style(i, col)
965964
kwds = kwargs.copy()
966965

967-
_maybe_add_color(kwargs, style, i)
966+
_maybe_add_color(kwds, style, i)
968967

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

pandas/tseries/tests/test_plotting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ def test_secondary_legend(self):
766766
colors.add(line.get_color())
767767

768768
# TODO: color cycle problems
769-
# self.assert_(len(colors) == 4)
769+
self.assert_(len(colors) == 4)
770770

771771
plt.clf()
772772
ax = fig.add_subplot(211)
@@ -790,7 +790,7 @@ def test_secondary_legend(self):
790790
colors.add(line.get_color())
791791

792792
# TODO: color cycle problems
793-
# self.assert_(len(colors) == 4)
793+
self.assert_(len(colors) == 4)
794794

795795
#non-ts
796796
df = tm.makeDataFrame()
@@ -805,7 +805,7 @@ def test_secondary_legend(self):
805805
colors.add(line.get_color())
806806

807807
# TODO: color cycle problems
808-
# self.assert_(len(colors) == 4)
808+
self.assert_(len(colors) == 4)
809809

810810
plt.clf()
811811
ax = fig.add_subplot(211)
@@ -818,7 +818,7 @@ def test_secondary_legend(self):
818818
colors.add(line.get_color())
819819

820820
# TODO: color cycle problems
821-
# self.assert_(len(colors) == 4)
821+
self.assert_(len(colors) == 4)
822822

823823
@slow
824824
def test_format_date_axis(self):

0 commit comments

Comments
 (0)