How to draw a histogram with different bin colors based on value?

5 views (last 30 days)
Suppose I have a vector of [-2, 3, 1, 2, -1, -2], and I want to create a histogram that with red bins when the element of vector>0 and blue bins when <0.
How could I do that?
Thank you!

Accepted Answer

KSSV
KSSV on 16 May 2019
v = [-2, 3, 1, 2, -1, -2] ;
x = 1:length(v) ;
figure
hold on
bar(x(v<0),v(v<0),'r')
bar(x(v>0),v(v>0),'b')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!