How can I fill between two curves, one color for when the test curve is above and a different color when it is below?
조회 수: 7 (최근 30일)
이전 댓글 표시
I'm hoping my aspirations for plotting aren't too ambitious for what MATLAB can handle. This is what I'm plotting right now. figure('Name','PTV'); hold on;plot(x,Base_Values.PTV{1},'--k','LineWidth',1.2);cellfun(@plot,Test_Case.PTV);
The cell Test_Case.PTV can have many elements or just one. How could I compare a plot from Test_Case.PTV to the first plot and have the area shaded red if it is above the first plot and green if below?
I would prefer to have a hatched fill, but I don't believe that's built into MATLAB, so I think I would need an outside function. But can this fill easily be done with two different colors for the corresponding areas between two curves?
And if so, I'm hoping it can be down for multiple elements at a time, although it will be quite messy, but I hope that the filled areas can be turned off with the plotted curve through plot browser. If that is not possible, I could create my function to only plot one at a time.
댓글 수: 0
답변 (3개)
KSSV
2016년 7월 8일
x = linspace(0,2*pi) ;
y = sin(x) ;
% plot(x,y) ; hold on
%%negative sine
idx1 = y<0 ;
x1 = x(idx1) ;
y1 = y(idx1) ;
x1 = [x1 x1(1)] ; % make a closed curve
y1 = [y1 y1(1)] ;
patch(x1,y1,'r','edgecolor','none') ;
% Positive sine
idx2 = y>0 ;
x2 = x(idx2) ;
y2 = y(idx2) ;
x2 = [x2 x2(1)] ; % make a closed curve
y2 = [y2 y2(1)] ;
patch(x2,y2,'g','edgecolor','none') ;
You may set 'edgecolor' option to 'none'. so that there wont be any connecting line shown.
댓글 수: 0
KSSV
2016년 6월 29일
x = linspace(0,2*pi) ;
y = sin(x) ;
plot(x,y) ; hold on
%%negative sine
idx1 = y<0 ;
x1 = x(idx1) ;
y1 = y(idx1) ;
x1 = [x1 x1(1)] ; % make a closed curve
y1 = [y1 y1(1)] ;
patch(x1,y1,'r') ;
% Positive sine
idx2 = y>0 ;
x2 = x(idx2) ;
y2 = y(idx2) ;
x2 = [x2 x2(1)] ; % make a closed curve
y2 = [y2 y2(1)] ;
patch(x2,y2,'g') ;
I hope the above example code might help you to get what you want...
댓글 수: 5
Star Strider
2016년 7월 6일
I need to have ‘x’, ‘y1’ and ‘y2’. Without them, I can’t reproduce your plot.
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
