Skip to content

Commit

Permalink
Add pad to stat_ecdf
Browse files Browse the repository at this point in the history
closes #725
  • Loading branch information
has2k1 committed Jan 4, 2024
1 parent ab5bda0 commit fd4ab67
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ title: Changelog
- Requires python >= 3.9
- The name of the calculated aesthetic of
[](:class:`~plotnine.stats.stat_function`) changed from `y` to `fx`.
- [](:class:`~plotnine.stats.stat_ecdf`) has gained the `pad` parameter. The
default is set to `True`, which pads the domain with `-inf` and `inf` so
that the ECDF does not have discontinuities at the extremes. To get the
behaviour, set `pad` to `False`. ({{< issue 725 >}})

### New

Expand Down
7 changes: 7 additions & 0 deletions plotnine/stats/stat_ecdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class stat_ecdf(stat):
n : int, default=None
This is the number of points to interpolate with.
If :py:`None`, do not interpolate.
pad : bool, default=True
If True, pad the domain with `-inf` and `+inf` so that
ECDF does not have discontinuities at the extremes.
See Also
--------
Expand All @@ -42,6 +45,7 @@ class stat_ecdf(stat):
"position": "identity",
"na_rm": False,
"n": None,
"pad": True,
}
DEFAULT_AES = {"y": after_stat("ecdf")}
CREATES = {"ecdf"}
Expand All @@ -56,6 +60,9 @@ def compute_group(cls, data, scales, **params):
else:
x = np.linspace(data["x"].min(), data["x"].max(), params["n"])

if params["pad"]:
x = np.hstack([-np.inf, x, np.inf])

ecdf = ECDF(data["x"])(x)
res = pd.DataFrame({"x": x, "ecdf": ecdf})
return res
Binary file modified tests/baseline_images/test_aes/stat_ecdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_stat_ecdf/computed_y_column.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_stat_ecdf/ecdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/test_stat_ecdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def test_ecdf():
assert p == "ecdf"


def test_ecdf_no_pad():
p = ggplot(data, aes("x")) + stat_ecdf(size=2, pad=False)

assert p == "ecdf_no_pad"


def test_computed_y_column():
p = (
ggplot(data, aes("x"))
Expand Down

0 comments on commit fd4ab67

Please sign in to comment.