# Unable to get a normalized density plot!

**URL:** <https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251>\
**Category:** General Usage\
**Tags:** statsplots\
**Created:** [May 18, 2022, 10:35am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251 "2022-05-18T10:35:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![devanshu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devanshu/32/37104_2.png) [@devanshu](https://discourse.julialang.org/u/devanshu)\
**Post date:** [May 18, 2022, 10:35am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/1 "2022-05-18T10:35:46Z")

</div>

Hi, I am trying to get the density plot of a data set. I am using the `StatsPlots` package for this. My code looks like this `density(data, fill=(0, 0.5), normalize=true)`. But I still don’t get a normalized plot.  
 ![out](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c4ddddcfc2f0914ddebf0cc7c924aa0f477f1ca.png)  
I wonder what’s wrong?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 18, 2022, 10:39am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/2 "2022-05-18T10:39:49Z")

</div>

I think there is a misconception on the notion of normalization. Density plots can sometimes exceed 1

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 18, 2022, 10:39am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/3 "2022-05-18T10:39:55Z")

</div>

Looks pretty normalized to me. It’s a density after all, which should integrate to 1.

---

<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:** [May 18, 2022, 10:44am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/4 "2022-05-18T10:44:47Z")

</div>

Based on my very coarse quadrature routine this looks about right:

```julia
julia> (0.0145 - 0.012)*400
1.0000000000000002

```

---

<div class="post-metadata">

**Author:** ![devanshu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devanshu/32/37104_2.png) [@devanshu](https://discourse.julialang.org/u/devanshu)\
**Post date:** [May 18, 2022, 10:47am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/5 "2022-05-18T10:47:55Z")

</div>

You are right, I framed the question amiss! Actually what I want is this plot to look like a probability density function.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 18, 2022, 10:57am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/6 "2022-05-18T10:57:59Z")

</div>

But it [does](https://en.wikipedia.org/wiki/Probability_density_function)!

Are you maybe trying to plot the probabilities to be in the finite intervals [x,x+\Delta x], [x+\Delta x,x+2\Delta x] and so on for some _finite_ \Delta x?  
In that case, fit a histogram and normalize it to probability:

```julia
import LinearAlgebra: normalize

hist = normalize(fit(Histogram, data), mode=:probability)
plot(hist)

```

---

<div class="post-metadata">

**Author:** ![devanshu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devanshu/32/37104_2.png) [@devanshu](https://discourse.julialang.org/u/devanshu)\
**Post date:** [May 18, 2022, 11:22am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/7 "2022-05-18T11:22:05Z")

</div>

> [@skleinbo](#):
>
> Are you maybe trying to plot the probabilities to be in the finite intervals [x,x+\Delta x] [x,x+Δx]

Well sort of, I just wanted my y-axis to look like probabilities, ie, less than one.

> [@skleinbo](#):
>
> fit a histogram and normalize it to probability:

This works, although I was hoping if I could get a smooth pdf like plot, like above, and not histograms.  
I tried this:

```julia
w = hist.weights
e = hist.edges[1]
x = [(e[i]+e[i+1])/2 for i in 1:length(e)-1];
plot(x, w, fill=(0,0.5))

```

But this doesn’t look smooth:

![out](https://global.discourse-cdn.com/julialang/original/3X/6/c/6c1e3cd2a87111c7b061f97cb851ce7e0a37f059.png)

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 18, 2022, 11:43am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/8 "2022-05-18T11:43:39Z")

</div>

> [@devanshu](#):
>
> I just wanted my y-axis to look like probabilities

Why? It may superficially look like a probability, but it won’t be. What would it be normalized to? The maximum?  
There is really nothing wrong with a PDF becoming larger than one. It’s expected. If that give you or your audience pause, I advise to first go and get comfortable with that. I apologize if that last sentence comes of as condescending. It is not meant to be.

Ultimately, the absolute value of a PDF isn’t very interesting. If what you want is to show the shape of the distribution, consider not putting ticks on the y-axis in the first place.

---

<div class="post-metadata">

**Author:** ![devanshu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devanshu/32/37104_2.png) [@devanshu](https://discourse.julialang.org/u/devanshu)\
**Post date:** [May 18, 2022, 11:47am UTC](https://discourse.julialang.org/t/unable-to-get-a-normalized-density-plot/81251/9 "2022-05-18T11:47:44Z")

</div>

Thanks got it!
