Skip to content

Added ind and bw_method kwargs to KdePlot #4316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
5 changes: 5 additions & 0 deletions doc/source/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
25 changes: 21 additions & 4 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand All @@ -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)
Expand Down