Skip to content

Commit 29b3bb5

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 29b3bb5

File tree

1 file changed

+54
-82
lines changed

1 file changed

+54
-82
lines changed

pandas/plotting/_core.py

+54-82
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,48 @@ def orientation(self):
13791379
else:
13801380
return 'vertical'
13811381

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

13831425
class KdePlot(HistPlot):
13841426
_kind = 'kde'
@@ -2616,47 +2658,10 @@ def hist(self, bins=10, **kwds):
26162658
"""
26172659
return self(kind='hist', bins=bins, **kwds)
26182660

2619-
def kde(self, bw_method=None, ind=None, **kwds):
2620-
"""
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-
--------
2661+
@Appender(_kde_docstring % {
2662+
'this-datatype': 'Series',
2663+
'sibling-datatype': 'DataFrame',
2664+
'examples': """
26602665
Given a Series of points randomly sampled from an unknown
26612666
distribution, estimate its distribution using KDE with automatic
26622667
bandwidth determination and plot the results, evaluating them at
@@ -2690,6 +2695,8 @@ def kde(self, bw_method=None, ind=None, **kwds):
26902695
26912696
>>> ax = s.plot.kde(ind=[1, 2, 3, 4, 5])
26922697
"""
2698+
})
2699+
def kde(self, bw_method=None, ind=None, **kwds):
26932700
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)
26942701

26952702
density = kde
@@ -2852,47 +2859,10 @@ def hist(self, by=None, bins=10, **kwds):
28522859
"""
28532860
return self(kind='hist', by=by, bins=bins, **kwds)
28542861

2855-
def kde(self, bw_method=None, ind=None, **kwds):
2856-
"""
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-
--------
2862+
@Appender(_kde_docstring % {
2863+
'this-datatype': 'DataFrame',
2864+
'sibling-datatype': 'Series',
2865+
'examples': """
28962866
Given several Series of points randomly sampled from unknown
28972867
distributions, estimate their distribution using KDE with automatic
28982868
bandwidth determination and plot the results, evaluating them at
@@ -2929,6 +2899,8 @@ def kde(self, bw_method=None, ind=None, **kwds):
29292899
29302900
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
29312901
"""
2902+
})
2903+
def kde(self, bw_method=None, ind=None, **kwds):
29322904
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)
29332905

29342906
density = kde

0 commit comments

Comments
 (0)