How do I fill the area between two plots in a loglog plot?

2 views (last 30 days)
I am trying to fill in the area between the confidence interval of my pwelch. This is the code I am using:
[px1, f, conf1] = pwelch(det_total_b1,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
[px2, f, conf2] = pwelch(det_total_b2,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
[px3, f, conf3] = pwelch(det_total_b3,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
[px4, f, conf4] = pwelch(det_total_b4,hanning(nfft),round(0.5*nfft),nfft,fs,'ConfidenceLevel',0.95);
loglog (f,px1,'Color',[0 204/256 102/256])
hold on
loglog (f,px2,'Color',[1 102/256 102/256])
loglog (f,px3,'Color',[0 128/256 1])
loglog (f,px4,'Color',[1 153/256 51/256])
patch([f fliplr(f)],[conf1(:,1) fliplr(conf1(:,2))],[0.85 0.85 0.85])
set(gca, 'XScale', 'log', 'YScale','log')
But instead of filling the area in gray, what it does is plotting the confidence interval limits as black lines (figure attached). I can't understand why it is doing that.
Thanks for the help in advance!
  2 Comments
Star Strider
Star Strider on 31 Oct 2019
I can’t run your code:
Unrecognized function or variable 'det_total_b1'.
Larissa Perez
Larissa Perez on 31 Oct 2019
Hi, thanks for the quick feedback.
det_total_b1 is my data. It is just a detrended time-series. I guess you can run it by creating a random time-series, just to check if the patch works?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 31 Oct 2019
You have one fundamental error. Your matrices are (Nx2), so using fliplr on each column is pointless. You need to use flipud and vertically concatenate them:
patch([f; flipud(f)],[conf1(:,1); flipud(conf1(:,2))],[0.85 0.85 0.85]);
Perhaps the problem is with your data or what you are plotting, although when I simulated it with random (rand) column vectors, the data were real and greater than zero.
Using patch with loglog plots works in other contexts, for example:
x = sort(rand(10, 1));
y = sort(rand(10, 1));
figure
patch([x; flipud(x)], [y; flipud(y+0.2)], 'r')
set(gca, 'XScale','log', 'YScale','log')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!