Kernel density estimation plot with median and quartiles

6 ビュー (過去 30 日間)
Tamir Eisenstein
Tamir Eisenstein 2019 年 2 月 7 日
回答済み: Tamir Eisenstein 2019 年 2 月 9 日
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

採用された回答

Rik
Rik 2019 年 2 月 8 日
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')

その他の回答 (1 件)

Tamir Eisenstein
Tamir Eisenstein 2019 年 2 月 9 日
Thank a lot, Rik!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by