Reputation: 704
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?
Upvotes: 0
Views: 671
Reputation: 18177
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
Upvotes: 2