필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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!!

채택된 답변

Star Strider
Star Strider 2020년 7월 24일
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
MS
MS 2020년 7월 24일
Thank you very much for helping me. It worked for me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by