Skip to content

Commit 092e216

Browse files
committed
added weights argument in _plot function and modified scipy.stats.gaussian_kde accordingly
1 parent f732749 commit 092e216

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pandas/plotting/_core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ def hist(
14491449
def kde(
14501450
self,
14511451
bw_method: Literal["scott", "silverman"] | float | Callable | None = None,
1452+
weights: np.ndarray | None = None,
14521453
ind: np.ndarray | int | None = None,
14531454
**kwargs,
14541455
) -> PlotAccessor:
@@ -1470,6 +1471,9 @@ def kde(
14701471
'scott', 'silverman', a scalar constant or a callable.
14711472
If None (default), 'scott' is used.
14721473
See :class:`scipy.stats.gaussian_kde` for more information.
1474+
weights : NumPy array, optional
1475+
Weights of datapoints. This must be the same shape as dataset.
1476+
If None (default), the samples are assumed to be equally weighted.
14731477
ind : NumPy array or int, optional
14741478
Evaluation points for the estimated PDF. If None (default),
14751479
1000 equally spaced points are used. If `ind` is a NumPy array, the
@@ -1560,7 +1564,7 @@ def kde(
15601564
15611565
>>> ax = df.plot.kde(ind=[1, 2, 3, 4, 5, 6])
15621566
"""
1563-
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)
1567+
return self(kind="kde", bw_method=bw_method, weights=weights, ind=ind, **kwargs)
15641568

15651569
density = kde
15661570

pandas/plotting/_matplotlib/hist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ def _plot( # type: ignore[override]
269269
y: np.ndarray,
270270
style=None,
271271
bw_method=None,
272+
weights=None,
272273
ind=None,
273274
column_num=None,
274275
stacking_id: int | None = None,
@@ -277,7 +278,7 @@ def _plot( # type: ignore[override]
277278
from scipy.stats import gaussian_kde
278279

279280
y = remove_na_arraylike(y)
280-
gkde = gaussian_kde(y, bw_method=bw_method)
281+
gkde = gaussian_kde(y, bw_method=bw_method, weights=weights)
281282

282283
y = gkde.evaluate(ind)
283284
lines = MPLPlot._plot(ax, ind, y, style=style, **kwds)

0 commit comments

Comments
 (0)