Skip to content

Commit 69cd5fb

Browse files
tommyodTomAugspurger
authored andcommitted
DOC: Exposed arguments in plot.kde (#19229)
* Exposed arguments in plot.kde, added number of sample points as option * Added a test for plot.kde with as an integer * Added whatsnew. Fixed flake8 errors. Used is_integer to infer type. * Updated scipy reference * Added test, rewrote whatsnew, removed import * Changed from Series to DataFrame in doc * Fixed PEP8 errors in test file * Fixed typo which made tests crash
1 parent cd6510d commit 69cd5fb

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ Plotting
536536

537537
- :func: `DataFrame.plot` now raises a ``ValueError`` when the ``x`` or ``y`` argument is improperly formed (:issue:`18671`)
538538
- Bug in formatting tick labels with ``datetime.time()`` and fractional seconds (:issue:`18478`).
539-
-
539+
- :meth:`Series.plot.kde` has exposed the args ``ind`` and ``bw_method`` in the docstring (:issue:`18461`). The argument ``ind`` may now also be an integer (number of sample points).
540540
-
541541

542542
Groupby/Resample/Rolling

pandas/plotting/_core.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,10 @@ def _get_ind(self, y):
13981398
sample_range = np.nanmax(y) - np.nanmin(y)
13991399
ind = np.linspace(np.nanmin(y) - 0.5 * sample_range,
14001400
np.nanmax(y) + 0.5 * sample_range, 1000)
1401+
elif is_integer(self.ind):
1402+
sample_range = np.nanmax(y) - np.nanmin(y)
1403+
ind = np.linspace(np.nanmin(y) - 0.5 * sample_range,
1404+
np.nanmax(y) + 0.5 * sample_range, self.ind)
14011405
else:
14021406
ind = self.ind
14031407
return ind
@@ -2598,20 +2602,30 @@ def hist(self, bins=10, **kwds):
25982602
"""
25992603
return self(kind='hist', bins=bins, **kwds)
26002604

2601-
def kde(self, **kwds):
2605+
def kde(self, bw_method=None, ind=None, **kwds):
26022606
"""
26032607
Kernel Density Estimate plot
26042608
26052609
Parameters
26062610
----------
2611+
bw_method: str, scalar or callable, optional
2612+
The method used to calculate the estimator bandwidth. This can be
2613+
'scott', 'silverman', a scalar constant or a callable.
2614+
If None (default), 'scott' is used.
2615+
See :class:`scipy.stats.gaussian_kde` for more information.
2616+
ind : NumPy array or integer, optional
2617+
Evaluation points. If None (default), 1000 equally spaced points
2618+
are used. If `ind` is a NumPy array, the kde is evaluated at the
2619+
points passed. If `ind` is an integer, `ind` number of equally
2620+
spaced points are used.
26072621
`**kwds` : optional
26082622
Keyword arguments to pass on to :py:meth:`pandas.Series.plot`.
26092623
26102624
Returns
26112625
-------
26122626
axes : matplotlib.AxesSubplot or np.array of them
26132627
"""
2614-
return self(kind='kde', **kwds)
2628+
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)
26152629

26162630
density = kde
26172631

@@ -2766,20 +2780,30 @@ def hist(self, by=None, bins=10, **kwds):
27662780
"""
27672781
return self(kind='hist', by=by, bins=bins, **kwds)
27682782

2769-
def kde(self, **kwds):
2783+
def kde(self, bw_method=None, ind=None, **kwds):
27702784
"""
27712785
Kernel Density Estimate plot
27722786
27732787
Parameters
27742788
----------
2789+
bw_method: str, scalar or callable, optional
2790+
The method used to calculate the estimator bandwidth. This can be
2791+
'scott', 'silverman', a scalar constant or a callable.
2792+
If None (default), 'scott' is used.
2793+
See :class:`scipy.stats.gaussian_kde` for more information.
2794+
ind : NumPy array or integer, optional
2795+
Evaluation points. If None (default), 1000 equally spaced points
2796+
are used. If `ind` is a NumPy array, the kde is evaluated at the
2797+
points passed. If `ind` is an integer, `ind` number of equally
2798+
spaced points are used.
27752799
`**kwds` : optional
27762800
Keyword arguments to pass on to :py:meth:`pandas.DataFrame.plot`.
27772801
27782802
Returns
27792803
-------
27802804
axes : matplotlib.AxesSubplot or np.array of them
27812805
"""
2782-
return self(kind='kde', **kwds)
2806+
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)
27832807

27842808
density = kde
27852809

pandas/tests/plotting/test_series.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,16 @@ def test_kde_kwargs(self):
621621
if not self.mpl_ge_1_5_0:
622622
pytest.skip("mpl is not supported")
623623

624-
from numpy import linspace
625-
_check_plot_works(self.ts.plot.kde, bw_method=.5,
626-
ind=linspace(-100, 100, 20))
624+
sample_points = np.linspace(-100, 100, 20)
625+
_check_plot_works(self.ts.plot.kde, bw_method='scott', ind=20)
626+
_check_plot_works(self.ts.plot.kde, bw_method=None, ind=20)
627+
_check_plot_works(self.ts.plot.kde, bw_method=None, ind=np.int(20))
628+
_check_plot_works(self.ts.plot.kde, bw_method=.5, ind=sample_points)
627629
_check_plot_works(self.ts.plot.density, bw_method=.5,
628-
ind=linspace(-100, 100, 20))
630+
ind=sample_points)
629631
_, ax = self.plt.subplots()
630-
ax = self.ts.plot.kde(logy=True, bw_method=.5,
631-
ind=linspace(-100, 100, 20), ax=ax)
632+
ax = self.ts.plot.kde(logy=True, bw_method=.5, ind=sample_points,
633+
ax=ax)
632634
self._check_ax_scales(ax, yaxis='log')
633635
self._check_text_labels(ax.yaxis.get_label(), 'Density')
634636

0 commit comments

Comments
 (0)