Skip to content
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

Improvements to ecdfplot #453

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsPlots"
uuid = "f3b207a7-027a-5e70-b257-86293d7955fd"
version = "0.14.23"
version = "0.14.24"

[deps]
Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
Expand Down
38 changes: 31 additions & 7 deletions src/ecdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@
# ---------------------------------------------------------------------------
# empirical CDF

@recipe function f(ecdf::StatsBase.ECDF)
seriestype := :steppost
function allsortedunique(x)
return all(eachindex(x)[1:end-1]) do i
@inbounds x[i] < x[i + 1]
end
end

@recipe function f(ecdf::StatsBase.ECDF; npoints=nothing)
seriestype --> :steppost
legend --> :topleft
x = [ecdf.sorted_values[1]; ecdf.sorted_values]
if :weights in propertynames(ecdf) && !isempty(ecdf.weights)
# support StatsBase versions >v0.32.0
y = [0; cumsum(ecdf.weights) ./ sum(ecdf.weights)]
if npoints !== nothing
x = [-Inf; range(extrema(ecdf)...; length=npoints)]
else
y = range(0, 1; length = length(x))
xnonunique = ecdf.sorted_values
xunique = allsortedunique(xnonunique) ? xnonunique : unique(xnonunique)
x = [-Inf; xunique]
end
y = ecdf(x)
x, y
end


"""
ecdfplot(x; npoints = nothing)

Plot the empirical cumulative distribution function (ECDF) of `x`.

By default, the ECDF is evaluated at the unique points in `x`. If `npoints` is provided,
it is instead evaluated on a uniform grid of length `npoints` between the extrema of `x`.
This is useful when `x` is large.

```julia-repl
julia> ecdfplot(randn(100))

julia> ecdfplot(randn(1_000_000); npoints=100, seriestype=:path)
```
"""
@userplot ECDFPlot

recipetype(::Val{:ecdfplot}, args...) = ECDFPlot(args)
@recipe function f(p::ECDFPlot)
x = p.args[1]
Expand Down