Change bar graph color by sign

5 ビュー (過去 30 日間)
Lutrinae
Lutrinae 2020 年 11 月 10 日
コメント済み: Lutrinae 2020 年 11 月 16 日
I have a 502x2 column matrix made up of B = [datenum, data]. There are positive nad negative data values, and I want to make all of the positive values red and all of the negative values blue, similar to the figure midway down this page: https://niwa.co.nz/climate/information-and-resources/southern-annular-mode
My code so far is just:
bar(B(:,1), B(:,2))
Is there a straightforward way to do this?
Thank you!

採用された回答

Karthik Ravishankar
Karthik Ravishankar 2020 年 11 月 10 日
bar(B(B(:,2)>=0,1), B(B(:,2)>=0,2),'FaceColor','b')
hold on
bar(B(B(:,2)<0,1), B(B(:,2)<0,2),'FaceColor','r')
  1 件のコメント
Lutrinae
Lutrinae 2020 年 11 月 16 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2020 年 11 月 10 日
x=B(:,1)';
y=B(:,1)';
b=bar(x,y,'r');
b.FaceColor='Flat';
b.CData(y<0,:)=repmat([0 0 1],nnz(y<0),1);

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by