user1790813
user1790813

Reputation: 704

Color the same line using two different colors

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

Answers (1)

Adriaan
Adriaan

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

enter image description here

Upvotes: 2

Related Questions