使用两种不同颜色为同一条线着色

时间:2015-10-10 20:42:14

标签: matlab plot matlab-figure

我必须绘制线性函数并以一种所有负值为红色且正值为蓝色的方式对其进行着色。

这只是我想问的一个例子。

概括,是否可以用与函数其余部分不同的颜色为函数的特定间隔着色,而不必做不同的图?

1 个答案:

答案 0 :(得分:2)

没有。为负值创建一个红色的绘图,使用hold on,然后使用蓝色绘制正值。

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