Skip to content

Commit 48658fb

Browse files
committed
Merge pull request #4316 from khgitting/kde_kwargs
Added ind and bw_method kwargs to KdePlot
2 parents 4cf2030 + aec85d0 commit 48658fb

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

doc/source/release.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ pandas 0.13
3030
**Release date:** not-yet-released
3131

3232
**New features**
33+
- ``plot(kind='kde')`` now accepts the optional parameters ``bw_method`` and
34+
``ind``, passed to scipy.stats.gaussian_kde() (for scipy >= 0.11.0) to set
35+
the bandwidth, and to gkde.evaluate() to specify the indicies at which it
36+
is evaluated, respecttively. See scipy docs.
3337

3438
- Added ``isin`` method to DataFrame (:issue:`4211`)
3539
- Clipboard functionality now works with PySide (:issue:`4282`)

doc/source/v0.13.0.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ Enhancements
163163
from pandas import offsets
164164
td + offsets.Minute(5) + offsets.Milli(5)
165165

166+
- ``plot(kind='kde')`` now accepts the optional parameters ``bw_method`` and
167+
``ind``, passed to scipy.stats.gaussian_kde() (for scipy >= 0.11.0) to set
168+
the bandwidth, and to gkde.evaluate() to specify the indicies at which it
169+
is evaluated, respecttively. See scipy docs.
170+
166171
.. _whatsnew_0130.refactoring:
167172

168173
Internal Refactoring

pandas/tests/test_graphics.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,18 @@ def test_kde(self):
257257
_check_plot_works(self.ts.plot, kind='kde')
258258
_check_plot_works(self.ts.plot, kind='density')
259259
ax = self.ts.plot(kind='kde', logy=True)
260-
self.assert_(ax.get_yscale() == 'log')
260+
self.assertEqual(ax.get_yscale(), 'log')
261261

262+
@slow
263+
def test_kde_kwargs(self):
264+
_skip_if_no_scipy()
265+
from numpy import linspace
266+
_check_plot_works(self.ts.plot, kind='kde', bw_method=.5, ind=linspace(-100,100,20))
267+
_check_plot_works(self.ts.plot, kind='density', bw_method=.5, ind=linspace(-100,100,20))
268+
ax = self.ts.plot(kind='kde', logy=True, bw_method=.5, ind=linspace(-100,100,20))
269+
self.assertEqual(ax.get_yscale(), 'log')
270+
271+
@slow
262272
def test_kde_color(self):
263273
_skip_if_no_scipy()
264274
ax = self.ts.plot(kind='kde', logy=True, color='r')

pandas/tools/plotting.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,11 +1136,15 @@ def _get_marked_label(self, label, col_num):
11361136

11371137

11381138
class KdePlot(MPLPlot):
1139-
def __init__(self, data, **kwargs):
1139+
def __init__(self, data, bw_method=None, ind=None, **kwargs):
11401140
MPLPlot.__init__(self, data, **kwargs)
1141+
self.bw_method=bw_method
1142+
self.ind=ind
11411143

11421144
def _make_plot(self):
11431145
from scipy.stats import gaussian_kde
1146+
from scipy import __version__ as spv
1147+
from distutils.version import LooseVersion
11441148
plotf = self._get_plot_function()
11451149
colors = self._get_colors()
11461150
for i, (label, y) in enumerate(self._iter_data()):
@@ -1149,10 +1153,23 @@ def _make_plot(self):
11491153

11501154
label = com.pprint_thing(label)
11511155

1152-
gkde = gaussian_kde(y)
1156+
if LooseVersion(spv) >= '0.11.0':
1157+
gkde = gaussian_kde(y, bw_method=self.bw_method)
1158+
else:
1159+
gkde = gaussian_kde(y)
1160+
if self.bw_method is not None:
1161+
msg = ('bw_method was added in Scipy 0.11.0.' +
1162+
' Scipy version in use is %s.' % spv)
1163+
warnings.warn(msg)
1164+
11531165
sample_range = max(y) - min(y)
1154-
ind = np.linspace(min(y) - 0.5 * sample_range,
1155-
max(y) + 0.5 * sample_range, 1000)
1166+
1167+
if self.ind is None:
1168+
ind = np.linspace(min(y) - 0.5 * sample_range,
1169+
max(y) + 0.5 * sample_range, 1000)
1170+
else:
1171+
ind = self.ind
1172+
11561173
ax.set_ylabel("Density")
11571174

11581175
y = gkde.evaluate(ind)

0 commit comments

Comments
 (0)