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 views (last 30 days)
Show older comments
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 Comments
Answers (3)
KSSV
on 8 Jul 2016
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 Comments
KSSV
on 29 Jun 2016
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 Comments
Star Strider
on 6 Jul 2016
I need to have ‘x’, ‘y1’ and ‘y2’. Without them, I can’t reproduce your plot.
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!