Filling the area between curves (loglog)

조회 수: 12 (최근 30일)
V.D-C
V.D-C 2020년 9월 1일
답변: V.D-C 2020년 9월 2일
Hello,
I would like to fill the area between the two lines (on the interval 10^-2 & 10^-1):
I tried creating my own polygon using "patch" but I wondered if there is any better and more straightforward option ?
I tried using "fill" with different options but I couldn't make it work.
load('Area_glaciers_scandinavia.mat');
% Create log bins that span from 0.01 to 10000 km2
cc = 10.^(-2:0.1:4);
% Create the histogram of those bins (count nb of glacier/bin)
H = hist(area,cc);
figure()
loglog(cc,H)
idx(1) = find(roundn(cc,-4)==0.1259); %put the boundaries of the linear part of the curve)
idx(2)= find(roundn(cc,-2)==19.95);
%use polyfit
%we use log10 because we are looking for the exponents
P = polyfit(log10(cc(idx)), log10(H(idx)),1);
cf = polyval(P,log10(cc));
hold on
loglog(cc,10.^cf);
  댓글 수: 2
KSSV
KSSV 2020년 9월 1일
Show us the code how you have plotted this. So that people can pick up that code and help you.
V.D-C
V.D-C 2020년 9월 1일
Sorry, I changed it

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

채택된 답변

V.D-C
V.D-C 2020년 9월 2일
Not very elegant, but I found that the best solution was to create manually:
x = [cc(1:idx(1)) fliplr(cc(1:idx(1)-1))];
y = [10.^cf(1:idx(1)) fliplr(H(1:idx(1)-1))];
patch(x,y,'g')

추가 답변 (1개)

David Wilson
David Wilson 2020년 9월 2일
편집: David Wilson 2020년 9월 2일
I too had some difficulty to do this elgantly. I changed the name of your data file, and commented out the material that didn't work.
%% patch in loglog plots
load('glacier_Data.mat');
%{
% Create log bins that span from 0.01 to 10000 km2
cc = 10.^(-2:0.1:4);
% Create the histogram of those bins (count nb of glacier/bin)
H = hist(area,cc);
figure()
%}
loglog(cc,H)
idx(1) = find(roundn(cc,-4)==0.1259); %put the boundaries of the linear part of the curve)
idx(2)= find(roundn(cc,-2)==19.95);
%use polyfit
%we use log10 because we are looking for the exponents
P = polyfit(log10(cc(idx)), log10(H(idx)),1);
cf = polyval(P,log10(cc));
loglog(cc,H, ...
cc,10.^cf);
grid on
Now try on a linear plot. (Just for testing)
%% Now do the shading on a linear plot
logcc = log10(cc); logH = log10(H);
idx = find(cc<1e-1);
xp = [logcc(idx), fliplr(logcc(idx))];
yp = [cf(idx), fliplr(logH(idx))]
plot(logcc, logH, ...
logcc, cf, '--')
patch(xp, yp, 'y')
Now try, as requested on a log plot. It's certainly not elegant, but does work.
%% Now try on log scale
loglog(cc,H, ...
cc,10.^cf);
h=patch(10.^xp, 10.^yp, 'y', 'faceAlpha', 0.5)
grid on
This gives the following:
  댓글 수: 1
V.D-C
V.D-C 2020년 9월 2일
Hi, thanks for your answer but I managed to do it without changing the code as you did, and it goes to the intersection between the two plots.
You showed me the way though ! Thank you for that !

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by