eqb
eqb

Reputation: 1770

Make plot with varied line color for specific y values on MATLAB

I want to make a plot such that for all y values greater than some y0, the plot will be shown in red. For all other values, it will be shown in blue. Is this possible in MATLAB? This would help draw distinct parts of a plot in different colors/settings for different conditions. I'm looking for something like

plot(x,y,{y>y0,'-r'}).

Upvotes: 0

Views: 1290

Answers (3)

Barney Szabolcs
Barney Szabolcs

Reputation: 12514

This question does not define what should happen at the limits.

But one good try is:

y1=y;
y1(y<=y0)=NaN;
plot(x,y,'b', x,y1,'r');

Upvotes: 0

B-Abbasi
B-Abbasi

Reputation: 823

you can separate the value which are greater than y0 and then plot the graph once for values less than y0 and second time for values greater than y0 by use of "hold on".

http://www.mathworks.com/help/matlab/ref/hold.html

Upvotes: 0

bla
bla

Reputation: 26069

try this:

 plot(x,y,'b',x(y>y0),y(y>y0),'r');

Upvotes: 2

Related Questions