# Bell Curve Overlay (Gadfly)

**URL:** <https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640>\
**Category:** Data\
**Created:** [June 3, 2020, 1:12am UTC](https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640 "2020-06-03T01:12:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Anderson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_anderson/32/9465_2.png) [@Chris\_Anderson](https://discourse.julialang.org/u/Chris_Anderson)\
**Post date:** [June 3, 2020, 1:12am UTC](https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640/1 "2020-06-03T01:12:12Z")

</div>

I have data that I’ve plotted using Geom.histogram. I’m trying to determine, visually, how closely it fits a normal distribution. I realize there are other more quantitative methods to test fit to a distribution, but for now I’m interested in the visual. How do I overlay a bell curve over my histogram? How do I set the parameters for that bell curve?  
Thank you.

---

<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:** [June 3, 2020, 2:18am UTC](https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640/2 "2020-06-03T02:18:57Z")

</div>

There’s an [example](http://gadflyjl.org/dev/gallery/geometries/#%5BGeom.histogram%5D(@ref)-1) in the docs, and here’s another:

```julia
using DataFrames, Distributions
D2 = [DataFrame(x=rand(Normal(μ,1), 500), μ="$(μ)") for μ in [-1, 1]]
alphs, cs = repeat([0.75, 0.85], outer=40), fill(colorant"black",2)

p2 = plot(vcat(D2...), x=:x, color=:μ, alpha=[alphs;alphs],
    layer([x->pdf(Normal(u, 1), x) for u in [-1,1]], -4, 4, color=cs),
    Geom.histogram(position=:identity, bincount=40, limits=(min=-4, max=4),
        density=true),
    Scale.color_discrete_manual("skyblue","moccasin")
)

```

![hist+density](https://global.discourse-cdn.com/julialang/original/3X/c/3/c33a3ac116a1652678c014ab40ded76498aa709c.png)

That plot has some new features (a new Gadfly release is is imminent), but you can have them now by doing `]add Gadfly#master`

---

<div class="post-metadata">

**Author:** ![Chris\_Anderson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_anderson/32/9465_2.png) [@Chris\_Anderson](https://discourse.julialang.org/u/Chris_Anderson)\
**Post date:** [June 3, 2020, 10:59pm UTC](https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640/3 "2020-06-03T22:59:12Z")

</div>

Thank you for your reply. I’ve been trying to recreate what I’ve seen here, but it doesn’t seem to be working. I ended up copy/pasting the example in the Gadfly docs and it throws an error:

ERROR: LoadError: MethodError: no method matching layer(::var"#21#22", ::Int64, ::Int64; color=ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}}[RGB{N0f8}(0.0,0  
.0,0.0)])

I’m not sure where to go from here. Thank you.

---

<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:** [June 3, 2020, 11:17pm UTC](https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640/4 "2020-06-03T23:17:11Z")

</div>

Oh I pointed you to the development docs (which will work if you’re on `Gadfly#master`, see note under above figure). Here’s the example from the [stable docs](http://gadflyjl.org/stable/gallery/geometries/#%5BGeom.histogram%5D(@ref)-1):

```julia
using Distributions, DataFrames
gamma = Gamma(2, 2)
Dgamma = DataFrame(x=rand(gamma, 10^4))
p4 = plot(Dgamma, Coord.cartesian(xmin=0, xmax=20),
    layer(x->pdf(gamma, x), 0, 20, Geom.line, Theme(default_color="black")),
    layer(x=:x, Geom.histogram(bincount=20, density=true, limits=(min=0,))),
    Theme(default_color="bisque") )

```

---

<div class="post-metadata">

**Author:** ![Chris\_Anderson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_anderson/32/9465_2.png) [@Chris\_Anderson](https://discourse.julialang.org/u/Chris_Anderson)\
**Post date:** [June 3, 2020, 11:37pm UTC](https://discourse.julialang.org/t/bell-curve-overlay-gadfly/40640/5 "2020-06-03T23:37:49Z")

</div>

Wonderful, thank you. This one works. I’ll play with it to get what I need out of it.
