Find max value in density plot

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)

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

5 Likes

Thanks!

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

kd = kde(data)
maximum(kd)

doesn’t have a matching method…

You can use bisection on pdf(kd, x)

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

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

1 Like

Thanks all, for your comments.

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:

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 that would be great to see implemented.