Skip to content

Commit a603336

Browse files
primitive KDE implementation, matches though
1 parent c0d30ff commit a603336

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

packages/python/plotly/plotly/express/_chart_types.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,7 @@ def kde(
575575
marginal=None,
576576
opacity=None,
577577
orientation=None,
578-
kdenorm=None, # TODO use this
579-
kernel=None, # TODO use this
580578
bw_method=None, # TODO use this
581-
bw_adjust=None, # TODO use this
582579
log_x=False,
583580
log_y=False,
584581
range_x=None,

packages/python/plotly/plotly/express/_core.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ def build_dataframe(args, constructor):
13231323
value_name = None # will likely be "value" in wide_mode
13241324
hist2d_types = [go.Histogram2d, go.Histogram2dContour]
13251325
hist1d_orientation = (
1326-
constructor == go.Histogram or "ecdfmode" in args or "kernel" in args
1326+
constructor == go.Histogram or "ecdfmode" in args or "bw_method" in args
13271327
)
13281328
if constructor in cartesians:
13291329
if wide_x and wide_y:
@@ -2100,6 +2100,19 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
21002100
elif args["ecdfnorm"] == "percent":
21012101
group[var] = 100.0 * group[var] / group_sum
21022102

2103+
if "bw_method" in args:
2104+
from scipy.stats import gaussian_kde
2105+
2106+
base = args["x"] if args["orientation"] == "v" else args["y"]
2107+
var = args["x"] if args["orientation"] == "h" else args["y"]
2108+
bw = args.get("bw_method")
2109+
group = group.sort_values(by=base)
2110+
2111+
kernel = gaussian_kde(
2112+
dataset=group[base], weights=group[var], bw_method=bw
2113+
)
2114+
group[var] = kernel.evaluate(group[base])
2115+
21032116
patch, fit_results = make_trace_kwargs(
21042117
args, trace_spec, group, mapping_labels.copy(), sizeref
21052118
)

packages/python/plotly/plotly/express/_doc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,11 @@
577577
"If `'complementary'`, the CCDF is plotted such that values represent data above the point.",
578578
"If `'reversed'`, a variant of the CCDF is plotted such that values represent data at or above the point.",
579579
],
580-
kernel=["TODO"], # kde
581-
kdenorm=["TODO"], # kde
582-
bw_method=["TODO"], # kde
583-
bw_adjust=["TODO"], # kde
580+
bw_method=[
581+
"str, scalar or callable (default `'scott'`)",
582+
"If str, must be one of `'scott'` or `'silverman'`.",
583+
"Passed to `scipy.stats.gaussian_kde`.",
584+
],
584585
)
585586

586587

0 commit comments

Comments
 (0)