# Can't Get BarChart to use ColorFunction

I'm trying to get a BarChart to colour negative valued bars red and positive valued bars green but I'm having a time trying to do it. My latest attempt is:

data = {0.34, 0.04, 1.07, -0.54, -2.4, 0.44};
t = Piecewise[{{# >= 0, Green}}, Red] &;
BarChart[data, ChartLayout -> "Stepped", BarSpacing -> None,
ColorFunction -> t,
ColorFunctionScaling -> False]


However this still colours the bars in the default colour. May someone tell me what I am doing wrong. I thought this would be pretty simple. I'm not getting an error. It just is ignoring the ColorFunction. I'm on 10.0.1

Thanks,

Edmund

You have inverted Piecewise arguments. Here is the right form:

data = {0.34, 0.04, 1.07, -0.54, -2.4, 0.44};
color=Piecewise[{{Green,# >= 0}}, Red] &;
BarChart[data
,ChartLayout -> "Stepped"
,BarSpacing -> None
,ColorFunction -> color
,ColorFunctionScaling -> False
]


Now it works:

I personaly prefer Which. Here is an example for accumulated color change using it:

color=Which[#<= 0,Red,True,Green]&;
BarChart[data
,ChartLayout -> "Stepped"
,BarSpacing -> None
,ChartStyle->color/@Accumulate@data
]


• Why is the last bar green ? – eldo Oct 8 '14 at 1:23
• @Murta Thanks, that works great. But do you know why Piecewise isn't working? Shouldn't it work the same as Which? Also, I prefer Piecewise as it is not a programming construct as Which is. – Edmund Oct 8 '14 at 1:30
• @Murta Why is the fourth bar red and the last one green ? – eldo Oct 8 '14 at 1:36
• @eldo, it's a positive variation. So, it's green... – Murta Oct 8 '14 at 1:37
• Why downvote? Someone knows? – Murta Oct 8 '14 at 1:42