Painting the area of difference between two lines

2 Ansichten (letzte 30 Tage)
Woonsup Choi
Woonsup Choi am 11 Feb. 2016
Kommentiert: Star Strider am 11 Feb. 2016
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.

Akzeptierte Antwort

Star Strider
Star Strider am 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’.
  2 Kommentare
Woonsup Choi
Woonsup Choi am 11 Feb. 2016
Thanks again. I had to adjust 'idxs' and 'idxe'.
Star Strider
Star Strider am 11 Feb. 2016
My pleasure.
I anticipated you would.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by