Skip to content

Commit

Permalink
DOC: extract similarities of kde docstrings
Browse files Browse the repository at this point in the history
The `DataFrame.plot.kde` and `Series.plot.kde` now use a common
docstring, for which the differences are inserted.
  • Loading branch information
jonas-schulze committed Mar 11, 2018
1 parent f197aea commit 11e146f
Showing 1 changed file with 59 additions and 82 deletions.
141 changes: 59 additions & 82 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,51 @@ def orientation(self):
return 'vertical'


_kde_docstring = \
"""
Generate Kernel Density Estimate plot using Gaussian kernels.
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
way to estimate the probability density function (PDF) of a random
variable. This function uses Gaussian kernels and includes automatic
bandwith determination.
.. _kernel density estimation:
https://en.wikipedia.org/wiki/Kernel_density_estimation
Parameters
----------
bw_method : str, scalar or callable, optional
The method used to calculate the estimator bandwidth. This can be
'scott', 'silverman', a scalar constant or a callable.
If None (default), 'scott' is used.
See :class:`scipy.stats.gaussian_kde` for more information.
ind : NumPy array or integer, optional
Evaluation points for the estimated PDF. If None (default),
1000 equally spaced points are used. If `ind` is a NumPy array, the
KDE is evaluated at the points passed. If `ind` is an integer,
`ind` number of equally spaced points are used.
**kwds : optional
Additional keyword arguments are documented in
:meth:`pandas.%(this-datatype)s.plot`.
Returns
-------
axes : matplotlib.AxesSubplot or np.array of them
See Also
--------
scipy.stats.gaussian_kde : Representation of a kernel-density
estimate using Gaussian kernels. This is the function used
internally to estimate the PDF.
%(sibling-datatype)s.plot.kde : Generate a KDE plot for a
%(sibling-datatype)s.
Examples
--------
%(examples)s
"""

class KdePlot(HistPlot):
_kind = 'kde'
orientation = 'vertical'
Expand Down Expand Up @@ -2616,47 +2661,11 @@ def hist(self, bins=10, **kwds):
"""
return self(kind='hist', bins=bins, **kwds)

def kde(self, bw_method=None, ind=None, **kwds):
@Appender(_kde_docstring % {
'this-datatype': 'Series',
'sibling-datatype': 'DataFrame',
'examples':
"""
Generate Kernel Density Estimate plot using Gaussian kernels.
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
way to estimate the probability density function (PDF) of a random
variable. This function uses Gaussian kernels and includes automatic
bandwith determination.
.. _kernel density estimation:
https://en.wikipedia.org/wiki/Kernel_density_estimation
Parameters
----------
bw_method : str, scalar or callable, optional
The method used to calculate the estimator bandwidth. This can be
'scott', 'silverman', a scalar constant or a callable.
If None (default), 'scott' is used.
See :class:`scipy.stats.gaussian_kde` for more information.
ind : NumPy array or integer, optional
Evaluation points for the estimated PDF. If None (default),
1000 equally spaced points are used. If `ind` is a NumPy array, the
KDE is evaluated at the points passed. If `ind` is an integer,
`ind` number of equally spaced points are used.
**kwds : optional
Additional keyword arguments are documented in
:meth:`pandas.Series.plot`.
Returns
-------
axes : matplotlib.AxesSubplot or np.array of them
See Also
--------
scipy.stats.gaussian_kde : Representation of a kernel-density
estimate using Gaussian kernels. This is the function used
internally to estimate the PDF.
DataFrame.plot.kde : Generate a KDE plot for a DataFrame.
Examples
--------
Given a Series of points randomly sampled from an unknown
distribution, estimate its distribution using KDE with automatic
bandwidth determination and plot the results, evaluating them at
Expand Down Expand Up @@ -2689,7 +2698,9 @@ def kde(self, bw_method=None, ind=None, **kwds):
:context: close-figs
>>> ax = s.plot.kde(ind=[1, 2, 3, 4, 5])
"""
""".strip()
})
def kde(self, bw_method=None, ind=None, **kwds):
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)

density = kde
Expand Down Expand Up @@ -2852,47 +2863,11 @@ def hist(self, by=None, bins=10, **kwds):
"""
return self(kind='hist', by=by, bins=bins, **kwds)

def kde(self, bw_method=None, ind=None, **kwds):
@Appender(_kde_docstring % {
'this-datatype': 'DataFrame',
'sibling-datatype': 'Series',
'examples':
"""
Generate Kernel Density Estimate plot using Gaussian kernels.
In statistics, `kernel density estimation`_ (KDE) is a non-parametric
way to estimate the probability density function (PDF) of a random
variable. This function uses Gaussian kernels and includes automatic
bandwith determination.
.. _kernel density estimation:
https://en.wikipedia.org/wiki/Kernel_density_estimation
Parameters
----------
bw_method : str, scalar or callable, optional
The method used to calculate the estimator bandwidth. This can be
'scott', 'silverman', a scalar constant or a callable.
If None (default), 'scott' is used.
See :class:`scipy.stats.gaussian_kde` for more information.
ind : NumPy array or integer, optional
Evaluation points for the estimated PDF. If None (default),
1000 equally spaced points are used. If `ind` is a NumPy array, the
KDE is evaluated at the points passed. If `ind` is an integer,
`ind` number of equally spaced points are used.
**kwds : optional
Additional keyword arguments are documented in
:meth:`pandas.DataFrame.plot`.
Returns
-------
axes : matplotlib.AxesSubplot or np.array of them
See Also
--------
scipy.stats.gaussian_kde : Representation of a kernel-density
estimate using Gaussian kernels. This is the function used
internally to estimate the PDF.
Series.plot.kde : Generate a KDE plot for a Series.
Examples
--------
Given several Series of points randomly sampled from unknown
distributions, estimate their distribution using KDE with automatic
bandwidth determination and plot the results, evaluating them at
Expand Down Expand Up @@ -2928,7 +2903,9 @@ def kde(self, bw_method=None, ind=None, **kwds):
:context: close-figs
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
"""
""".strip()
})
def kde(self, bw_method=None, ind=None, **kwds):
return self(kind='kde', bw_method=bw_method, ind=ind, **kwds)

density = kde
Expand Down

0 comments on commit 11e146f

Please sign in to comment.