x-y plot: how to shade the difference between two lines in multiple colors

kindly help me to fill the diffrence with two diffrent colur in a x y plot as shown in the below example figure?
I need to shade postive(red color) and negative diffrences(blue color) in diffrent colors. Kindly give your suggesttions
t= 0:.01:1
AA = cos(t)
BB= sin(t)
diff = AA-BB
plot(t,AA)
hold on
plot(t,BB)
patch([t fliplr(t)], [AA fliplr(BB)], 'r')
hold off
%%% any suggestions Please!!

 Accepted Answer

Correct idea, not quite the correct approach.
Try this:
t= 0:.01:1;
AA = cos(t);
BB= sin(t);
Lv = AA > BB; % Logical Vector
figure
plot(t,AA)
hold on
plot(t,BB)
patch([t(Lv) fliplr(t(Lv))], [AA(Lv) fliplr(BB(Lv))], 'r')
patch([t(~Lv) fliplr(t(~Lv))], [AA(~Lv) fliplr(BB(~Lv))], 'g')
hold off
producing:
.This uses ‘logical indexing’ to separate the two regions.
The independent variable (‘t’ here) may need greater resolution for more complicated curves for this approach to work correctly. If the curves are data, use linspace to create the indpendent variable vector with greater resolution, and interp1 to interpolate the data to it.

4 Comments

Dear star rider,
Thank you very much for the suggestions. I treid to use the same code for the data. But, i was not sucessful implementing the same to the data. I am attaching two data for your refernce. Would you mind to help me out in this?
D = load('Liftb60.mat');
DD = load('Liftpb60.mat');
wingArea = .5*1000*.01*(.3*.1);
t = 1/10000:1/10000:2;
figure
AAA = -D.Lift_b/wingArea;
BBB = -DD.Lift_pb/wingArea;
t = 1/10000:1/10000:2;
plot(t,AAA,'r','LineWidth', 6);
hold on
plot(t,BBB,'b','LineWidth', 6);
Lv = AAA > BBB; % Logical Vector
patch([t(Lv) fliplr(t(Lv))], [AAA(Lv) fliplr(BBB(Lv))], 'm')
patch([t(~Lv) fliplr(t(~Lv))], [AAA(~Lv) fliplr(BBB(~Lv))], 'k')
hold off
Three changes:
t = (1/10000:1/10000:2).'; % Must Be A Column Vector To Match The Data
and:
patch([t(Lv); flipud(t(Lv))], [AAA(Lv); flipud(BBB(Lv))], 'm', 'EdgeColor','none') % Use ‘flipud’ & Vertical Concatenation For Column Vectors
patch([t(~Lv); flipud(t(~Lv))], [AAA(~Lv); flipud(BBB(~Lv))], 'k', 'EdgeColor','none') % Use ‘flipud’ & Vertical Concatenation For Column Vectors
When I ran it with those changes, it produced what appears to be the correct result.
I do not completely understand your data, however the patch calls work.
.
Thank you very much for helping me. It worked for me.

Sign in to comment.

More Answers (0)

Asked:

MS
on 23 Jul 2020

Commented:

on 24 Jul 2020

Community Treasure Hunt

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

Start Hunting!