From aec85d0b07c9d4b4288880176dd77c4975cea6a8 Mon Sep 17 00:00:00 2001 From: Kyle Hausmann Date: Sun, 21 Jul 2013 18:24:06 -0400 Subject: [PATCH] Added ind and bw_method kwargs to KdePlot --- doc/source/release.rst | 4 ++++ doc/source/v0.13.0.txt | 5 +++++ pandas/tests/test_graphics.py | 12 +++++++++++- pandas/tools/plotting.py | 25 +++++++++++++++++++++---- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 929c167cd1340..56d51183a1834 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -30,6 +30,10 @@ pandas 0.13 **Release date:** not-yet-released **New features** + - ``plot(kind='kde')`` now accepts the optional parameters ``bw_method`` and + ``ind``, passed to scipy.stats.gaussian_kde() (for scipy >= 0.11.0) to set + the bandwidth, and to gkde.evaluate() to specify the indicies at which it + is evaluated, respecttively. See scipy docs. - Added ``isin`` method to DataFrame (:issue:`4211`) - Clipboard functionality now works with PySide (:issue:`4282`) diff --git a/doc/source/v0.13.0.txt b/doc/source/v0.13.0.txt index 16ae57310dae7..43ad0c32b0bfe 100644 --- a/doc/source/v0.13.0.txt +++ b/doc/source/v0.13.0.txt @@ -163,6 +163,11 @@ Enhancements from pandas import offsets td + offsets.Minute(5) + offsets.Milli(5) + - ``plot(kind='kde')`` now accepts the optional parameters ``bw_method`` and + ``ind``, passed to scipy.stats.gaussian_kde() (for scipy >= 0.11.0) to set + the bandwidth, and to gkde.evaluate() to specify the indicies at which it + is evaluated, respecttively. See scipy docs. + .. _whatsnew_0130.refactoring: Internal Refactoring diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index 3e74b71441410..9d570d8bcbadf 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -257,8 +257,18 @@ def test_kde(self): _check_plot_works(self.ts.plot, kind='kde') _check_plot_works(self.ts.plot, kind='density') ax = self.ts.plot(kind='kde', logy=True) - self.assert_(ax.get_yscale() == 'log') + self.assertEqual(ax.get_yscale(), 'log') + @slow + def test_kde_kwargs(self): + _skip_if_no_scipy() + from numpy import linspace + _check_plot_works(self.ts.plot, kind='kde', bw_method=.5, ind=linspace(-100,100,20)) + _check_plot_works(self.ts.plot, kind='density', bw_method=.5, ind=linspace(-100,100,20)) + ax = self.ts.plot(kind='kde', logy=True, bw_method=.5, ind=linspace(-100,100,20)) + self.assertEqual(ax.get_yscale(), 'log') + + @slow def test_kde_color(self): _skip_if_no_scipy() ax = self.ts.plot(kind='kde', logy=True, color='r') diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 3fbdedf0c5dd0..9f6f3b08ee508 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -1136,11 +1136,15 @@ def _get_marked_label(self, label, col_num): class KdePlot(MPLPlot): - def __init__(self, data, **kwargs): + def __init__(self, data, bw_method=None, ind=None, **kwargs): MPLPlot.__init__(self, data, **kwargs) + self.bw_method=bw_method + self.ind=ind def _make_plot(self): from scipy.stats import gaussian_kde + from scipy import __version__ as spv + from distutils.version import LooseVersion plotf = self._get_plot_function() colors = self._get_colors() for i, (label, y) in enumerate(self._iter_data()): @@ -1149,10 +1153,23 @@ def _make_plot(self): label = com.pprint_thing(label) - gkde = gaussian_kde(y) + if LooseVersion(spv) >= '0.11.0': + gkde = gaussian_kde(y, bw_method=self.bw_method) + else: + gkde = gaussian_kde(y) + if self.bw_method is not None: + msg = ('bw_method was added in Scipy 0.11.0.' + + ' Scipy version in use is %s.' % spv) + warnings.warn(msg) + sample_range = max(y) - min(y) - ind = np.linspace(min(y) - 0.5 * sample_range, - max(y) + 0.5 * sample_range, 1000) + + if self.ind is None: + ind = np.linspace(min(y) - 0.5 * sample_range, + max(y) + 0.5 * sample_range, 1000) + else: + ind = self.ind + ax.set_ylabel("Density") y = gkde.evaluate(ind)