0
votes

I have to plot a linear function and color it in a way that all negative values are red and the positive values are blue.

This is just an example of what I want to ask.

Generalizing, is it possible to color specific intervals of a function in a different color than that of the rest of the function, without having to do different plots?

1
No. Create a red coloured-plot for the negative values, use hold on and then a blue coloured plot for the positive values.Adriaan
Thanks for the answer.user1790813
Hold on on for segmentation...kpie

1 Answers

2
votes

No. Create a red coloured plot for the negative values, use hold on and then a blue coloured plot for the positive values.

x = [-10:0.1:10].'; %//range
x1 = x(x<=0); %//negative values and zero
x2 = x(x>=0); %//positive values and zero
figure; %//open figure
hold on %// plot things in the same figure
plot(x1,x1,'r') %//plot negative values
plot(x2,x2,'b') %//plot positive values

enter image description here