How to fill the area between two line in a log log plot

Hi,
I want to shade the area between the lower and upper curve. I attached the dat. file so you can use this. The following simple code has been used to generate the given figure.
load limit.dat
a=limit(:,1);
upper=limit(:,2);
lower=limit(:,3);
avg=limit(:,4);
loglog(a,avg,a,upper,a,lower)
Can anybody please come with an idea. Thanks in advance.
Cheers!!
Pavel

 Accepted Answer

You need to edit your data to be only those greater than zero (negative and zero values will not plot on a loglog plot anyway, so you lose nothing by eliminating them), then a simple patch call works:
fidi = fopen('Mainul Hoque limit.txt','r');
limitc = textscan(fidi, '%f%f%f%f', 'CollectOutput',1);
limit = limitc{:};
a=limit(:,1);
upper=limit(:,2);
lower=limit(:,3);
avg=limit(:,4);
idx = upper>0 & lower>0 & avg>0; % Eliminate Negative & Zero Data
figure(1)
patch([a(idx)' fliplr(a(idx)')], [lower(idx)' fliplr(upper(idx)')], [0.8 0.8 0.8])
hold on
plot(a(idx),avg(idx), a(idx),upper(idx), a(idx),lower(idx))
hold off
set(gca, 'XScale', 'log', 'YScale','log')
I used a light gray triple [0.8 0.8 0.8] for the fill colour. Use whatever colour works for you.

6 Comments

My pleasure.
That was an interesting problem. I learned something from it.
Hello everyone,
I followed the solution proposed by Star Strider. However I end up with a problem. The patch area isn't filled, just like on this topic https://ch.mathworks.com/matlabcentral/answers/361234-i-am-using-the-patch-function-and-i-am-trying-to-set-a-facecolor-in-2017a-and-nothing-is-working-d. My patch or fill function work just fine normally, they just don't seem to be working correctly for loglolog or semilogs plots. Do you have any solutions ? I tried the solution proposed in that topic I just mentionned, but it didn't work.
Here is my code:
I have 2 subplots here, the second one strictly following what Star Strider suggested. The first one is slightly different, plotting first a blue curve that is forcing the axis to be loglog anyway. In both case i manage to draw the patch, but it is not filled! Only the contour appear!
By the way, there shouldn't be any negative value in my datas.
load('examplecode')
lower=Pxxc(:,1)';
upper=Pxxc(:,2)';
figure
title(['Amplitude : Power Spectrum Content'])
subplot(2,1,1)
loglog(f2,Pxx,'linewidth',1.5);
hold on;
patch([f2', fliplr(f2')],[lower, fliplr(upper)], [0.8 0.8 0.8])
%set(gca, 'XScale', 'log')
xlabel('Period (Years)'); ylabel('Spectral power (normalized)');
flab=get(gca,'xtick'); xlim([1/40 1/2.5]);
set(gca,'xtick', [1/40 1/20 1/10 1/5 1/2.5 ], ...
'xticklabel',[40 20 10 5 2.5 ]);
subplot(2,1,2)
lower=Pxxc(:,1)';
upper=Pxxc(:,2)';
patch([f2', fliplr(f2')],[lower, fliplr(upper)], [0.8 0.8 0.8])
set(gca, 'XScale', 'log', 'YScale','log')
xlabel('Period (Years)'); ylabel('Spectral power (normalized)');
flab=get(gca,'xtick'); xlim([1/40 1/2.5]);
set(gca,'xtick', [1/40 1/20 1/10 1/5 1/2.5 ], ...
'xticklabel',[40 20 10 5 2.5 ]);
Thank you for your help!!
Cheers,
Samexamplecode.jpg
For loglog plots, all values of every vector must be > 0.
f2(1) = 1E-5;
The reason is the the log of negative values are pure imaginary, and the log of 0 is -Inf. Only real, finite values plot.
I focussed on the PSD vector and I didn't think about checking the frequency vector. It works great now, with your help.
Thanks a lot and sorry for the inconvenience.
No worries.
A Vote would be appreciated!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!