Kernel density estimation plot with median and quartiles
5 views (last 30 days)
Show older comments
Hi,
I used this to generate a kernel density estimation plot:
[f,xi] = ksdensity(myvector);
figure
plot(xi,f);
How can I insert three vertical lines in the plot representing the median and 1st & 3rd quartiles?
Thanks,
Tamir
0 Comments
Accepted Answer
Rik
on 8 Feb 2019
I don't have the statistics toolbox, so I needed to generate some random data myself, but the code below should do what you need.
%generate f,xi data
gauss_fun=@(x,mu,sd) 1/sqrt(2*pi*sd^2)*exp(-(x-mu).^2/(2*sd^2));
xi=linspace(-8,8,500);
f=gauss_fun(xi,0,2);
%plot original plot
figure(1),clf(1)
plot(xi,f)
y=cumsum(f);y=y/y(end);
Q1=find(y>0.25,1);
Q2=find(y>0.5 ,1);
Q3=find(y>0.75,1);
%plot the 3 lines
hold on
plot(xi(Q1)*[1 1],[0 f(Q1)],'r')
plot(xi(Q2)*[1 1],[0 f(Q2)],'r')
plot(xi(Q3)*[1 1],[0 f(Q3)],'r')
0 Comments
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!