首页 文章

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

提问于
浏览
0

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

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

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

1 回答

  • 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

相关问题