Change bar graph color by sign
조회 수: 10 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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개)
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);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Bar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!