Skip to content

Commit 11e146f

Browse files
committed
DOC: extract similarities of kde docstrings
The `DataFrame.plot.kde` and `Series.plot.kde` now use a common docstring, for which the differences are inserted.
1 parent f197aea commit 11e146f

File tree

1 file changed

+59
-82
lines changed

1 file changed

+59
-82
lines changed

pandas/plotting/_core.py

+59-82
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,51 @@ def orientation(self):
13801380
return 'vertical'
13811381

13821382

1383+
_kde_docstring = \
1384+
"""
1385+
Generate Kernel Density Estimate plot using Gaussian kernels.
1386+
1387+
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
1388+
way to estimate the probability density function (PDF) of a random
1389+
variable. This function uses Gaussian kernels and includes automatic
1390+
bandwith determination.
1391+
1392+
.. _kernel density estimation:
1393+
https://en.wikipedia.org/wiki/Kernel_density_estimation
1394+
1395+
Parameters
1396+
----------
1397+
bw_method : str, scalar or callable, optional
1398+
The method used to calculate the estimator bandwidth. This can be
1399+
'scott', 'silverman', a scalar constant or a callable.
1400+
If None (default), 'scott' is used.
1401+
See :class:`scipy.stats.gaussian_kde` for more information.
1402+
ind : NumPy array or integer, optional
1403+
Evaluation points for the estimated PDF. If None (default),
1404+
1000 equally spaced points are used. If `ind` is a NumPy array, the
1405+
KDE is evaluated at the points passed. If `ind` is an integer,
1406+
`ind` number of equally spaced points are used.
1407+
**kwds : optional
1408+
Additional keyword arguments are documented in
1409+
:meth:`pandas.%(this-datatype)s.plot`.
1410+
1411+
Returns
1412+
-------
1413+
axes : matplotlib.AxesSubplot or np.array of them
1414+
1415+
See Also
1416+
--------
1417+
scipy.stats.gaussian_kde : Representation of a kernel-density
1418+
estimate using Gaussian kernels. This is the function used
1419+
internally to estimate the PDF.
1420+
%(sibling-datatype)s.plot.kde : Generate a KDE plot for a
1421+
%(sibling-datatype)s.
1422+
1423+
Examples
1424+
--------
1425+
%(examples)s
1426+
"""
1427+
13831428
class KdePlot(HistPlot):
13841429
_kind = 'kde'
13851430
orientation = 'vertical'
@@ -2616,47 +2661,11 @@ def hist(self, bins=10, **kwds):
26162661
"""
26172662
return self(kind='hist', bins=bins, **kwds)
26182663

2619-
def kde(self, bw_method=None, ind=None, **kwds):
2664+
@Appender(_kde_docstring % {
2665+
'this-datatype': 'Series',
2666+
'sibling-datatype': 'DataFrame',
2667+
'examples':
26202668
"""
2621-
Generate Kernel Density Estimate plot using Gaussian kernels.
2622-
2623-
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
2624-
way to estimate the probability density function (PDF) of a random
2625-
variable. This function uses Gaussian kernels and includes automatic
2626-
bandwith determination.
2627-
2628-
.. _kernel density estimation:
2629-
https://en.wikipedia.org/wiki/Kernel_density_estimation
2630-
2631-
Parameters
2632-
----------
2633-
bw_method : str, scalar or callable, optional
2634-
The method used to calculate the estimator bandwidth. This can be
2635-
'scott', 'silverman', a scalar constant or a callable.
2636-
If None (default), 'scott' is used.
2637-
See :class:`scipy.stats.gaussian_kde` for more information.
2638-
ind : NumPy array or integer, optional
2639-
Evaluation points for the estimated PDF. If None (default),
2640-
1000 equally spaced points are used. If `ind` is a NumPy array, the
2641-
KDE is evaluated at the points passed. If `ind` is an integer,
2642-
`ind` number of equally spaced points are used.
2643-
**kwds : optional
2644-
Additional keyword arguments are documented in
2645-
:meth:`pandas.Series.plot`.
2646-
2647-
Returns
2648-
-------
2649-
axes : matplotlib.AxesSubplot or np.array of them
2650-
2651-
See Also
2652-
--------
2653-
scipy.stats.gaussian_kde : Representation of a kernel-density
2654-
estimate using Gaussian kernels. This is the function used
2655-
internally to estimate the PDF.
2656-
DataFrame.plot.kde : Generate a KDE plot for a DataFrame.
2657-
2658-
Examples
2659-
--------
26602669
Given a Series of points randomly sampled from an unknown
26612670
distribution, estimate its distribution using KDE with automatic
26622671
bandwidth determination and plot the results, evaluating them at
@@ -2689,7 +2698,9 @@ def kde(self, bw_method=None, ind=None, **kwds):
26892698
:context: close-figs
26902699
26912700
>>> ax = s.plot.kde(ind=[1, 2, 3, 4, 5])
2692-
"""
2701+
""".strip()
2702+
})
2703+
def kde(self, bw_method=None, ind=None, **kwds):
26932704
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)
26942705

26952706
density = kde
@@ -2852,47 +2863,11 @@ def hist(self, by=None, bins=10, **kwds):
28522863
"""
28532864
return self(kind='hist', by=by, bins=bins, **kwds)
28542865

2855-
def kde(self, bw_method=None, ind=None, **kwds):
2866+
@Appender(_kde_docstring % {
2867+
'this-datatype': 'DataFrame',
2868+
'sibling-datatype': 'Series',
2869+
'examples':
28562870
"""
2857-
Generate Kernel Density Estimate plot using Gaussian kernels.
2858-
2859-
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
2860-
way to estimate the probability density function (PDF) of a random
2861-
variable. This function uses Gaussian kernels and includes automatic
2862-
bandwith determination.
2863-
2864-
.. _kernel density estimation:
2865-
https://en.wikipedia.org/wiki/Kernel_density_estimation
2866-
2867-
Parameters
2868-
----------
2869-
bw_method : str, scalar or callable, optional
2870-
The method used to calculate the estimator bandwidth. This can be
2871-
'scott', 'silverman', a scalar constant or a callable.
2872-
If None (default), 'scott' is used.
2873-
See :class:`scipy.stats.gaussian_kde` for more information.
2874-
ind : NumPy array or integer, optional
2875-
Evaluation points for the estimated PDF. If None (default),
2876-
1000 equally spaced points are used. If `ind` is a NumPy array, the
2877-
KDE is evaluated at the points passed. If `ind` is an integer,
2878-
`ind` number of equally spaced points are used.
2879-
**kwds : optional
2880-
Additional keyword arguments are documented in
2881-
:meth:`pandas.DataFrame.plot`.
2882-
2883-
Returns
2884-
-------
2885-
axes : matplotlib.AxesSubplot or np.array of them
2886-
2887-
See Also
2888-
--------
2889-
scipy.stats.gaussian_kde : Representation of a kernel-density
2890-
estimate using Gaussian kernels. This is the function used
2891-
internally to estimate the PDF.
2892-
Series.plot.kde : Generate a KDE plot for a Series.
2893-
2894-
Examples
2895-
--------
28962871
Given several Series of points randomly sampled from unknown
28972872
distributions, estimate their distribution using KDE with automatic
28982873
bandwidth determination and plot the results, evaluating them at
@@ -2928,7 +2903,9 @@ def kde(self, bw_method=None, ind=None, **kwds):
29282903
:context: close-figs
29292904
29302905
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
2931-
"""
2906+
""".strip()
2907+
})
2908+
def kde(self, bw_method=None, ind=None, **kwds):
29322909
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)
29332910

29342911
density = kde

0 commit comments

Comments
 (0)