# How do I plot the estimated cumulative density function of some samples?

**URL:** https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032
**Category:** General Usage
**Tags:** plotting, gadfly, plots
**Created:** [April 22, 2020, 4:03pm UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032 "2020-04-22T16:03:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [April 22, 2020, 4:03pm UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/1 "2020-04-22T16:03:15Z")

</div>

I have some samples of a random variable, and I want to plot their cdf. What’s the best way to do this?  
I have currently tried:

```julia
    g1 = [E(λ) for i = 1:n] # generate the samples
    using StatsBase
    cdf = ecdf(g1)
    xs = 0:10^-3:5
    display(plot(y = [cdf(i) for i in xs], x = xs) #gadfly

```

But this seems rather unidiomatic and inelegant. Any better ideas? Also, this only works for continuous variables, I think.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 22, 2020, 5:18pm UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/2 "2020-04-22T17:18:11Z")

</div>

I don’t think you need anything fancy here:

```julia
using Plots
n = 50
g1 = [randn() for i = 1:n] # generate the samples

p = plot(sort(g1), (1:n)./n, 
    xlabel = "sample", ylabel = "Probability", 
    title = "Empirical Cumluative Distribution", label = "")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/f/8fa5a8697e45c07f76d916e7947dc2c3525e2daf.png)

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [April 22, 2020, 5:52pm UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/3 "2020-04-22T17:52:14Z")

</div>

You could also use the `plot(function, lower, upper)` method from `Plots`:

```julia
using Plots, StatsBase
g1 = randn(100)
gcdf = ecdf(g1)
plot(x -> gcdf(x), 0, 5)

```

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [April 23, 2020, 12:02am UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/4 "2020-04-23T00:02:59Z")

</div>

There’s also [an example](http://gadflyjl.org/stable/gallery/geometries/#%5BGeom.histogram%5D(@ref)-1) (`Geom.histogram`) in the Gadfly docs showing a sample and theoretical pdf (the latter is done using a layer with function).

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 23, 2020, 1:08am UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/5 "2020-04-23T01:08:40Z")

</div>

> [@ElOceanografo](#):
>
> x → gcdf(x)

`x -> gcdf(x)` is the same as just `gcdf`  
🙂

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [April 23, 2020, 4:20pm UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/6 "2020-04-23T16:20:08Z")

</div>

Normally yes, but the `ECDF` object is not a function, so the `plot` doesn’t know to dispatch on it as such:

```julia
julia> plot(gcdf, 0, 5)
ERROR: Cannot convert ECDF{Array{Float64,1},Weights{Float64,Float64,Array{Float64,1}}} to series data for plotting

```

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [April 23, 2020, 5:03pm UTC](https://discourse.julialang.org/t/how-do-i-plot-the-estimated-cumulative-density-function-of-some-samples/38032/7 "2020-04-23T17:03:30Z")

</div>

is there a way to change the method signature of `plot` (either Gadfly’s or Plots’) to accommodate [function-like objects](https://docs.julialang.org/en/latest/manual/methods/#Function-like-objects-1) like ECDF?
