Kernel density estimation plot with median and quartiles

조회 수: 5 (최근 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