0
$\begingroup$

I want to control the color of individual bars by checking them against a threshold value. E.g., If[value<=threshold, color=red, color=blue]. I have been trying to get the "ColorFunction" to work but can't quite get the syntax working properly.

Example below based on previous work.

data = {6.73, 4.18, 4., 5.91, 6.9, 3.4, 5.13, 6.8, 7.5, 5.28, 4.25, 5.14, 6.71, 2.91, 6.48, 6.8, 4.54, 6.22, 5.27, 6.41, 3.39, 3.14, 4.24, 6.43, 8.55, 7.22, 7.14, 5.95, 5.63, 6.31, 5.34, 5.24, 6.34,5.51, 5.61, 6.4, 6.13, 5.26, 5.42, 5.62, 5.6, 6.23, 6.29, 5.29,7.69, 7.51};

threshold = 5;

Show[BarChart[data,BaseStyle -> {FontSize -> 14}, BarSpacing ->Medium, ColorFunction -> Function[{height}, ColorData[{"ThermometerColors","Reverse"}][height]]],Graphics[{Red, Thick,Dashed, Line[{{0, threshold}, {Length[data] + 1, threshold}}]}]]

Anything above the "threshold" value should be one color and everything below another. A bonus would be a third color for items within a specific delta of the threshold value (e.g, value>=threshold & value<(threshold+10%)).

$\endgroup$

marked as duplicate by Mr.Wizard graphics Oct 6 '17 at 19:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Oct 6 '17 at 19:12
0
$\begingroup$

You need to disable ColorFunctionScaling

threshold = 5;
Show[BarChart[data, BaseStyle -> {FontSize -> 14}, BarSpacing -> Medium, 
  ColorFunction -> (If[# < threshold*0.9, Green, If[# > threshold*1.1, Orange, Blue]] &), 
  ColorFunctionScaling -> False], 
  Graphics[{Red, Thick, Dashed, Line[{{0, threshold}, {Length[data] + 1, threshold}}]}]]

enter image description here

$\endgroup$
0
$\begingroup$
BarChart[data, BaseStyle -> {FontSize -> 14}, BarSpacing -> Medium, 
 ColorFunctionScaling -> False, 
 ColorFunction -> Function[{height}, Piecewise[{{Red, height <= threshold},
  {Blue, threshold < height <= 1.1 threshold}}, Orange]], 
 Epilog -> {Dashed, Line[{{0, threshold}, {Length[data] + 1, threshold}}], 
   Line[{{0, 1.1 threshold}, {Length[data] + 1, 1.1 threshold}}]}]

enter image description here

Alternatively, style individual data points:

styleddata = Style[#, Piecewise[{{Red, # <= threshold}, {Blue, 
        threshold < # <= 1.1 threshold}}, Yellow]] & /@ data;
BarChart[styleddata, BaseStyle -> {FontSize -> 14}, 
 BarSpacing -> Medium, Epilog -> {Red, Dashed, 
   Line[{{0, threshold}, {Length[data] + 1, threshold}}], 
   Line[{{0, 1.1 threshold}, {Length[data] + 1, 1.1 threshold}}]}]

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.