フィルターのクリア

How can I fill between two curves, but only when one curve is above the other?

6 ビュー (過去 30 日間)
Hello,
I'd like to fill the gap between two lines, but only when one line is above the other.
With the data file attached and the command:
plot(x,y1,'k',x,y2,'k:');
hold on;
xlim([x(1) x(end)]);
jbfill(x',y1',y2',[.8 .8 .8],'none',0,.5);
legend('y1','y2');
hold off;
I can plot:
Figure.png
which always fills the gap between y1 and y2. But I'd like it to be filled only when y1>y2.
Any help on this is very welcome.
Best,

採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 7 月 26 日
You can do a little trick:
plot(x,y1,'k',x,y2,'k:');
hold on;
xlim([x(1) x(end)]);
y1tmp = y1;
y1tmp(y1<y2) = y2(y1<y2);
jbfill(x',y1tmp',y2',[.8 .8 .8],'none',0,.5);
legend('y1','y2');
hold off;

その他の回答 (2 件)

KSSV
KSSV 2019 年 7 月 26 日
load Data.mat ;
plot(x,y1,'k',x,y2,'k:');
hold on;
xlim([x(1) x(end)]);
% jbfill(x',y1',y2',[.8 .8 .8],'none',0,.5);
legend('y1','y2');
hold off;
idx = y1>y2 ;
figure
X = [x(idx) ; flipud(x(idx))] ;
Y = [y1(idx) ; flipud(y2(idx))] ;
fill(X,Y,'r')

Vinicius Pereira Mateus Borges
Vinicius Pereira Mateus Borges 2019 年 7 月 26 日
Hi, I am new to MATLAB - I just started doing an online course a few weeks ago - so this is probably not a very good solution.
But it works!
plot(x,y1,'k',x,y2,'k:');
hold on;
xlim([x(1) x(end)]);
% initiate temporary variable
temp1 = zeros(1,numel(x));
temp2 = zeros(1,numel(x));
for i = 1:numel(x)
if y1(i) > y2(i)
temp1(i) = y1(i) % only store values of y1 if y1>y2
temp2(i) = y2(i) % only store values of y2 if y1>y2
end
end
jbfill(x',temp1,temp2,[.8 .8 .8],'none',0,.5); % only plot values when y1>y2
legend('y1','y2');
hold off;
shg

Community Treasure Hunt

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

Start Hunting!

Translated by