# Density and histograms

**URL:** https://discourse.julialang.org/t/density-and-histograms/115394
**Category:** New to Julia
**Tags:** plotting, plots, histogram
**Created:** [June 9, 2024, 3:10pm UTC](https://discourse.julialang.org/t/density-and-histograms/115394 "2024-06-09T15:10:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![LoneWolf1304](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lonewolf1304/32/207791_2.png) [@LoneWolf1304](https://discourse.julialang.org/u/LoneWolf1304)
#### Post date: [June 9, 2024, 3:10pm UTC](https://discourse.julialang.org/t/density-and-histograms/115394/1 "2024-06-09T15:10:56Z")

</div>

I am using Julia for plotting the density of a data using density(). I wanted to know if there is any way to obtain the data points of the density(), like the values that is plotted by density(). I can obtain the histogram heights but they vary from the density plot.  
Also, is there any easy way to find the density values of a given dataset?

---

<div class="post-metadata">

### Author: ![Yuan-Ru-Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuan-ru-lin/32/46068_2.png) [@Yuan-Ru-Lin](https://discourse.julialang.org/u/Yuan-Ru-Lin)
#### Post date: [June 9, 2024, 3:26pm UTC](https://discourse.julialang.org/t/density-and-histograms/115394/2 "2024-06-09T15:26:27Z")

</div>

Either normalize yourself

```julia
julia> using StatsBase
julia> h = fit(Histogram, rand(1000), 0:0.01:1);
julia> h.weights ./ sum(h.weights)
100-element Vector{Float64}:
 0.012
 0.01
 0.013
...

```

or use `LinearAlgebra.normalize`

```julia
julia> using LinearAlgebra
julia> normalize(h)
Histogram{Float64, 1, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}}}
edges:
  0.0:0.01:1.0
weights: [1.2, 1.0, 1.3, 0.9, 1.6, 0.7, 0.9, 1.0, 1.3, 0.7 … 1.2, 1.0, 0.8, 0.9, 0.9, 1.0, 1.3, 0.8, 0.8, 1.5]
closed: left
isdensity: true

```

---

<div class="post-metadata">

### Author: ![LoneWolf1304](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lonewolf1304/32/207791_2.png) [@LoneWolf1304](https://discourse.julialang.org/u/LoneWolf1304)
#### Post date: [June 9, 2024, 3:44pm UTC](https://discourse.julialang.org/t/density-and-histograms/115394/3 "2024-06-09T15:44:50Z")

</div>

Actually I was asking to find the density plot values.  
 ![photo_2024-06-09_21-14-16](https://global.discourse-cdn.com/julialang/original/3X/3/3/3384d2e6dd33f31078c80253ea10ad979dd36f70.jpeg)

Like in the image, the histogram plot has different heights on both side, but the density plot is almost symmetric. I wanted to obtain the values of the density distribution.

---

<div class="post-metadata">

### Author: ![Yuan-Ru-Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuan-ru-lin/32/46068_2.png) [@Yuan-Ru-Lin](https://discourse.julialang.org/u/Yuan-Ru-Lin)
#### Post date: [June 10, 2024, 1:15am UTC](https://discourse.julialang.org/t/density-and-histograms/115394/4 "2024-06-10T01:15:44Z")

</div>

How did you get the density? What package are you using?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [June 10, 2024, 4:56am UTC](https://discourse.julialang.org/t/density-and-histograms/115394/5 "2024-06-10T04:56:56Z")

</div>

There’s a simple density function in this package: [GitHub - m3g/EasyFit.jl: Easy interface for obtaining fits for 2D data](https://github.com/m3g/EasyFit.jl)

---

<div class="post-metadata">

### Author: ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)
#### Post date: [June 10, 2024, 3:49pm UTC](https://discourse.julialang.org/t/density-and-histograms/115394/6 "2024-06-10T15:49:55Z")

</div>

It is possible that the density is actually symmetric and that the difference between the peaks is simply an artifact owing to how the histogram is binned. The left hand side starts at -3000, while the right hand side ends at 3000+one bin width, and if the last and next-to-last bins are added together they would probably add up to close to the height of the opposite side.

If you look at the data itself, is it symmetric? It may be worthwhile to play around with different binning options (left/right/center justified, smaller bin sizes, etc) to see what effect that has. Or try a cumulative histogram since they do not have bin size artifacts.
