Painting the area of difference between two lines

조회 수: 2 (최근 30일)
Woonsup Choi
Woonsup Choi 2016년 2월 11일
댓글: Star Strider 2016년 2월 11일
I have a plot like the image. There are two occasions where the orange line is above the blue line (around 211 and 331 on the x-axis). I would like to paint the area between the two lines for the two occasions to highlight that the blue is below the orange.

채택된 답변

Star Strider
Star Strider 2016년 2월 11일
I don’t have your data, so I created my own:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices Of Argument Vector
x = linspace(0, 100, 250); % Independent Variable
y1 = exp(-0.05*x).*sin(2*pi*x/30); % First Dependent Variable
y2 = 0.5*exp(-0.07*x).*sin(2*pi*x/20); % Second Dependent Variable
yd = y1 - y2; % Difference (Use To Detect Zero-Crossings)
xc = zci(yd); % Zero Crossings
idxs = xc(2:2:end-1); % Start Indices Of Overlap
idxe = xc(3:2:end); % End Indices Of Overlap
figure(1)
plot(x, y1)
hold on
plot(x, y2)
for k1 = 1:size(idxs,1)
fill([x(idxs(k1):idxe(k1)) fliplr(x(idxs(k1):idxe(k1)))], [y1(idxs(k1):idxe(k1)) fliplr(y2(idxs(k1):idxe(k1)))], 'g')
end
hold off
grid
You should be able to adapt it to your data with little or no alteration, although you might need to adjust ‘idxs’ and ‘idxe’.
  댓글 수: 2
Woonsup Choi
Woonsup Choi 2016년 2월 11일
Thanks again. I had to adjust 'idxs' and 'idxe'.
Star Strider
Star Strider 2016년 2월 11일
My pleasure.
I anticipated you would.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by