# Find max value in density plot

**URL:** <https://discourse.julialang.org/t/find-max-value-in-density-plot/48950>\
**Category:** Statistics\
**Created:** [October 24, 2020, 7:25pm UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950 "2020-10-24T19:25:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 24, 2020, 7:25pm UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/1 "2020-10-24T19:25:47Z")

</div>

How do I get the max value of the graph in `density(data?)`  
(I want to plot a line on the plot to mark the mean value, as well as two other values. So I need to figure out about how big the plot area is in y-direction, so that the line covers the entire height of the plot(ish), but not more) (edited)

---

<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:** [October 24, 2020, 8:06pm UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/2 "2020-10-24T20:06:42Z")

</div>

Call the kernel density estimation manually  
[https://github.com/JuliaStats/KernelDensity.jl](https://github.com/JuliaStats/KernelDensity.jl)  
From that you can easily find the maximum.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 24, 2020, 8:27pm UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/3 "2020-10-24T20:27:01Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![GregCoyle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregcoyle/32/29663_2.png) [@GregCoyle](https://discourse.julialang.org/u/GregCoyle)\
**Post date:** [November 18, 2021, 1:57am UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/4 "2021-11-18T01:57:07Z")

</div>

> [@baggepinnen](#):
>
> From that you can easily find the maximum.

How, exactly? I can’t seem to find a way to access the internal array of the kde, and, something like

```julia
kd = kde(data)
maximum(kd)

```

doesn’t have a matching method…

---

<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:** [November 18, 2021, 5:12am UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/5 "2021-11-18T05:12:07Z")

</div>

You can use bisection on `pdf(kd, x) `

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 18, 2021, 12:24pm UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/6 "2021-11-18T12:24:48Z")

</div>

If you can fit a distribution using Distributions.jl, then you could compute its mode.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 18, 2021, 2:13pm UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/7 "2021-11-18T14:13:21Z")

</div>

> [@GregCoyle](#):
>
> can’t seem to find a way to access the internal array of the kde, and, something like

Its right in the read me of the KernelDensity.jl linked above

> The `UnivariateKDE` object `U` contains gridded coordinates ( `U.x` ) and the density estimate ( `U.density` ).

Just take the maximum of U.density

---

<div class="post-metadata">

**Author:** ![GregCoyle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregcoyle/32/29663_2.png) [@GregCoyle](https://discourse.julialang.org/u/GregCoyle)\
**Post date:** [November 19, 2021, 4:31am UTC](https://discourse.julialang.org/t/find-max-value-in-density-plot/48950/8 "2021-11-19T04:31:48Z")

</div>

Thanks all, for your comments.

> [@dlakelan](#):
>
> Just take the maximum of U.density

That’s helpful, and is what the OP asked for, but just to add a bit for other new users trying to find the mode of the KDE, from its maximum value:

```julia
U = kde(data)
imax = findmax(U.density)[2]
U_mode = U.x[imax]

```

More generally, I think this is related to an [open enhancement issue](https://github.com/JuliaStats/KernelDensity.jl/issues/58) that would be great to see implemented.
