Skip to content

Commit dfe6216

Browse files
primitive KDE implementation, matches though
1 parent 5f0ac8b commit dfe6216

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
@@ -579,10 +579,7 @@ def kde(
579579
marginal=None,
580580
opacity=None,
581581
orientation=None,
582-
kdenorm=None, # TODO use this
583-
kernel=None, # TODO use this
584582
bw_method=None, # TODO use this
585-
bw_adjust=None, # TODO use this
586583
log_x=False,
587584
log_y=False,
588585
range_x=None,

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ def build_dataframe(args, constructor):
13191319
value_name = None # will likely be "value" in wide_mode
13201320
hist2d_types = [go.Histogram2d, go.Histogram2dContour]
13211321
hist1d_orientation = (
1322-
constructor == go.Histogram or "ecdfmode" in args or "kernel" in args
1322+
constructor == go.Histogram or "ecdfmode" in args or "bw_method" in args
13231323
)
13241324
if constructor in cartesians:
13251325
if wide_x and wide_y:
@@ -2106,6 +2106,19 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
21062106
elif args["ecdfnorm"] == "percent":
21072107
group[var] = 100.0 * group[var] / group_sum
21082108

2109+
if "bw_method" in args:
2110+
from scipy.stats import gaussian_kde
2111+
2112+
base = args["x"] if args["orientation"] == "v" else args["y"]
2113+
var = args["x"] if args["orientation"] == "h" else args["y"]
2114+
bw = args.get("bw_method")
2115+
group = group.sort_values(by=base)
2116+
2117+
kernel = gaussian_kde(
2118+
dataset=group[base], weights=group[var], bw_method=bw
2119+
)
2120+
group[var] = kernel.evaluate(group[base])
2121+
21092122
patch, fit_results = make_trace_kwargs(
21102123
args, trace_spec, group, mapping_labels.copy(), sizeref
21112124
)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,11 @@
591591
"If `'complementary'`, the CCDF is plotted such that values represent data above the point.",
592592
"If `'reversed'`, a variant of the CCDF is plotted such that values represent data at or above the point.",
593593
],
594-
kernel=["TODO"], # kde
595-
kdenorm=["TODO"], # kde
596-
bw_method=["TODO"], # kde
597-
bw_adjust=["TODO"], # kde
594+
bw_method=[
595+
"str, scalar or callable (default `'scott'`)",
596+
"If str, must be one of `'scott'` or `'silverman'`.",
597+
"Passed to `scipy.stats.gaussian_kde`.",
598+
],
598599
)
599600

600601

0 commit comments

Comments
 (0)