Painting the area of difference between two lines

1 view (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 11 Feb 2016
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’.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!