How can I label the same line using different color

Hi, everyone,
I want describe the same line by different colors, for exaple,
a=0:0.01:2*pi; b=sin(a); plot(a,b,'r')
however, how to make the sine line red when its value is more than zero while
black if its value is less than zero.
Thanks.

回答 (1 件)

Matt Fig
Matt Fig 2012 年 9 月 11 日
編集済み: Matt Fig 2012 年 9 月 11 日

0 投票

One way to get the effect is to use two lines.
a = 0:0.01:2*pi;
b = sin(a);
b2 = b;
b(b<=0) = nan; % nan values are not plotted.
b2(b2>=0) = nan;
plot(a,b,'r',a,b2,'k')

カテゴリ

ヘルプ センター および File ExchangeLanguage Fundamentals についてさらに検索

タグ

質問済み:

2012 年 9 月 11 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by