How can I shade confidence intervals on a loglog plot?

조회 수: 26 (최근 30일)
Heidi Hirsh
Heidi Hirsh 2020년 2월 9일
댓글: Star Strider 2020년 2월 10일
I am trying to plot frequency spectra with 95% confidence intervals (upper and lower bounds). I am using pwelch to calculate power spectra then plotting using loglog. But so far I can only figure out how to plot the confidence intervals at line. I would like to shade the intervals instead. I think I could do so by shading between the power line and the confidence interval lines. Any ideas? This is my current code and I have attached a figure.
Thank you!
figure
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK1.zavg_pE(j,pkperiod))),[],[],nfft,288);
h=loglog(F,PxxU,F,PxxC,'--','color','k');
%h=loglog(F,PxxU);
hold on
grid on
set(gca,'fontsize',20)
xlabel('f (cpd)','fontsize',20)
ylabel('S_{along} (days)','fontsize',20)
title('Alongshore Velocity');
set(gca,'fontsize',16)
xlim([.1 10])
% legend('PK','PO','location','southwest')
legend('PK','PK upper','PK lower','location','southwest')

답변 (2개)

Hiro Yoshino
Hiro Yoshino 2020년 2월 10일
x = -10:0.1:10; % between intersections
yy1 =x.^2; %lower function
yy2 = -(x.^2-16); % upper function
x = [x,x]; % repeat x values
yy = [yy1,yy2]; % vector of upper & lower boundaries
fill(x,yy,'g') % fill area defined by x & yy in green
Hope this helps you out!
  댓글 수: 2
Heidi Hirsh
Heidi Hirsh 2020년 2월 10일
Thank you Hiro! I am wondering how I can apply this to a loglog plot? I think in my case yy = PxxC (the upper and lower confidence bounds).
I tried fill(loglog(F,PxxC,'g')) and loglog(fill(F,PxxC,'g'))
Neither works. Is there another way to combine the fill function with log axes?
Hiro Yoshino
Hiro Yoshino 2020년 2월 10일
I'm sure you can find a solution by referring to the documentation! Good luck.

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2020년 2월 10일
Try this:
x = linspace(0.1, 10, 100); % Create Data
y = sin(2*pi*x/5)+1.5; % Create Data
err = [y+0.2; y-0.2]; % Create Data
figure
plot(x, y) % Mean
hold on
patch([x, fliplr(x)], [err(1,:), fliplr(err(2,:))], 'g', 'FaceAlpha',0.5) % Shaded Confidence Intervals
hold off
set(gca, 'XScale','log', 'YScale','log')
producing:
1How can I shade confidence intervals on a loglog plot - 2020 02 09.png
  댓글 수: 2
Heidi Hirsh
Heidi Hirsh 2020년 2월 10일
Thank you Star Strider! I'm getting closer! But something weird is happening for the my yaxis values between ~0.25 and 1.75 where it is shading outside the confidence intervals instead of inside. I'm not sure why.
I incorporated your code like this:
figure
sgtitle('Current Spectra, (August 9 - October 2, NFFT=8192)')
P = .95; %confidence interval
clear nfft; nfft = 8192;
jdo = 1;
j=jdo
hold on
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK1.zavg_pE(j,pkperiod))),[],[],nfft,288);
% h=loglog(F,PxxU,F,PxxC,'--','color','k');
loglog(F,PxxU);
patch([F, fliplr(F)], [PxxC(:,1), fliplr(PxxC(:,2))],'g','FaceAlpha',0.5)
grid on
set(gca,'fontsize',20)
xlabel('f (cpd)','fontsize',20)
ylabel('S_{along} (days)','fontsize',20)
set(gca,'fontsize',16)
xlim([.1 10])
Star Strider
Star Strider 2020년 2월 10일
You can most likely resolve that problem by sorting all your data by the independent variable. It appears to be ‘wrapping’, so it returns to fhe first value at the end of the data.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by