Painting the area of difference between two lines

2 次查看(过去 30 天)
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’.

更多回答(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