Skip to content

ENH: Allow to plot weighted KDEs. #59337

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 12 commits into from
Jul 31, 2024
6 changes: 5 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,7 @@ def hist(
def kde(
self,
bw_method: Literal["scott", "silverman"] | float | Callable | None = None,
weights: np.ndarray | None = None,
ind: np.ndarray | int | None = None,
**kwargs,
) -> PlotAccessor:
Expand All @@ -1470,6 +1471,9 @@ def kde(
'scott', 'silverman', a scalar constant or a callable.
If None (default), 'scott' is used.
See :class:`scipy.stats.gaussian_kde` for more information.
weights : NumPy array, optional
Weights of datapoints. This must be the same shape as dataset.
If None (default), the samples are assumed to be equally weighted.
ind : NumPy array or int, optional
Evaluation points for the estimated PDF. If None (default),
1000 equally spaced points are used. If `ind` is a NumPy array, the
Expand Down Expand Up @@ -1560,7 +1564,7 @@ def kde(

>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
"""
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
return self(kind="kde", bw_method=bw_method, weights=weights, ind=ind, **kwargs)

density = kde

Expand Down
3 changes: 2 additions & 1 deletion pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def _plot( # type: ignore[override]
y: np.ndarray,
style=None,
bw_method=None,
weights=None,
ind=None,
column_num=None,
stacking_id: int | None = None,
Expand All @@ -277,7 +278,7 @@ def _plot( # type: ignore[override]
from scipy.stats import gaussian_kde

y = remove_na_arraylike(y)
gkde = gaussian_kde(y, bw_method=bw_method)
gkde = gaussian_kde(y, bw_method=bw_method, weights=weights)

y = gkde.evaluate(ind)
lines = MPLPlot._plot(ax, ind, y, style=style, **kwds)
Expand Down
Loading