How can I shade confidence intervals on a loglog plot?

73 views (last 30 days)
Heidi Hirsh
Heidi Hirsh on 9 Feb 2020
Commented: Star Strider on 10 Feb 2020
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!
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK1.zavg_pE(j,pkperiod))),[],[],nfft,288);
hold on
grid on
xlabel('f (cpd)','fontsize',20)
ylabel('S_{along} (days)','fontsize',20)
title('Alongshore Velocity');
xlim([.1 10])
% legend('PK','PO','location','southwest')
legend('PK','PK upper','PK lower','location','southwest')

Answers (2)

Hiro on 10 Feb 2020
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!
Hiro on 10 Feb 2020
I'm sure you can find a solution by referring to the documentation! Good luck.

Sign in to comment.

Star Strider
Star Strider on 10 Feb 2020
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
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')
1How can I shade confidence intervals on a loglog plot - 2020 02 09.png
Star Strider
Star Strider on 10 Feb 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!