Skip to content

Commit 809b238

Browse files
author
Chang She
committed
BUG: KDE plot fails with color kwarg pandas-dev#3187
1 parent 460f50d commit 809b238

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

pandas/tests/test_graphics.py

+9
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ def test_kde(self):
152152
ax = self.ts.plot(kind='kde', logy=True)
153153
self.assert_(ax.get_yscale() == 'log')
154154

155+
@slow
156+
def test_kde_color(self):
157+
_skip_if_no_scipy()
158+
_check_plot_works(self.ts.plot, kind='kde')
159+
_check_plot_works(self.ts.plot, kind='density')
160+
ax = self.ts.plot(kind='kde', logy=True, color='r')
161+
self.assert_(ax.get_lines()[0].get_color() == 'r')
162+
self.assert_(ax.get_lines()[1].get_color() == 'r')
163+
155164
@slow
156165
def test_autocorrelation_plot(self):
157166
from pandas.tools.plotting import autocorrelation_plot

pandas/tools/plotting.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,
236236

237237
def _label_axis(ax, kind='x', label='', position='top',
238238
ticks=True, rotate=False):
239-
239+
240240
from matplotlib.artist import setp
241241
if kind == 'x':
242242
ax.set_xlabel(label, visible=True)
@@ -247,14 +247,14 @@ def _label_axis(ax, kind='x', label='', position='top',
247247
setp(ax.get_xticklabels(), rotation=90)
248248
elif kind == 'y':
249249
ax.yaxis.set_visible(True)
250-
ax.set_ylabel(label, visible=True)
250+
ax.set_ylabel(label, visible=True)
251251
# ax.set_ylabel(a)
252252
ax.yaxis.set_ticks_position(position)
253253
ax.yaxis.set_label_position(position)
254-
return
255-
256-
257-
254+
return
255+
256+
257+
258258

259259

260260
def _gca():
@@ -1026,6 +1026,19 @@ def _get_style(self, i, col_name):
10261026

10271027
return style or None
10281028

1029+
def _get_colors(self):
1030+
import matplotlib.pyplot as plt
1031+
cycle = plt.rcParams.get('axes.color_cycle', list('bgrcmyk'))
1032+
if isinstance(cycle, basestring):
1033+
cycle = list(cycle)
1034+
colors = self.kwds.get('color', cycle)
1035+
return colors
1036+
1037+
def _maybe_add_color(self, colors, kwds, style, i):
1038+
has_color = 'color' in kwds
1039+
if has_color and (style is None or re.match('[a-z]+', style) is None):
1040+
kwds['color'] = colors[i % len(colors)]
1041+
10291042

10301043
class KdePlot(MPLPlot):
10311044
def __init__(self, data, **kwargs):
@@ -1034,6 +1047,7 @@ def __init__(self, data, **kwargs):
10341047
def _make_plot(self):
10351048
from scipy.stats import gaussian_kde
10361049
plotf = self._get_plot_function()
1050+
colors = self._get_colors()
10371051
for i, (label, y) in enumerate(self._iter_data()):
10381052
ax = self._get_ax(i)
10391053
style = self._get_style(i, label)
@@ -1049,6 +1063,7 @@ def _make_plot(self):
10491063
y = gkde.evaluate(ind)
10501064
kwds = self.kwds.copy()
10511065
kwds['label'] = label
1066+
self._maybe_add_color(colors, kwds, style, i)
10521067
if style is None:
10531068
args = (ax, ind, y)
10541069
else:
@@ -1119,19 +1134,6 @@ def _use_dynamic_x(self):
11191134

11201135
return (freq is not None) and self._is_dynamic_freq(freq)
11211136

1122-
def _get_colors(self):
1123-
import matplotlib.pyplot as plt
1124-
cycle = plt.rcParams.get('axes.color_cycle', list('bgrcmyk'))
1125-
if isinstance(cycle, basestring):
1126-
cycle = list(cycle)
1127-
colors = self.kwds.get('color', cycle)
1128-
return colors
1129-
1130-
def _maybe_add_color(self, colors, kwds, style, i):
1131-
has_color = 'color' in kwds
1132-
if has_color and (style is None or re.match('[a-z]+', style) is None):
1133-
kwds['color'] = colors[i % len(colors)]
1134-
11351137
def _make_plot(self):
11361138
import pandas.tseries.plotting as tsplot
11371139
# this is slightly deceptive

0 commit comments

Comments
 (0)