1
$\begingroup$

I have a bar chart for probabilities of getting a score in a dice game with unfair dice, need to show how probabilities vary with score and don't just constantly go up. I thought color would be a good way to do this.

So I tried to look up how to directly change colors of bars on a bar graph and found posts saying you couldn't, that you had to use a color scheme. Alright I thought, that will work, I need the higher probabilities on this chart to look different from the lower so a heatmap theme would work, but it turns out, theme only changes the bars by x position.

How do I make the color change based on Y values?

enter image description here

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ You are looking for ColorFunction. $\endgroup$
    – swish
    Commented Apr 14, 2017 at 21:34

2 Answers 2

2
$\begingroup$

This will color your bar with the hue defined by the value of each bar:

BarChart[
 Table[
  {Style[zz = RandomReal[], FaceForm[Hue[zz]]]}, {20}]]
$\endgroup$
2
  • $\begingroup$ It's a new feature of 11.1? $\endgroup$
    – yode
    Commented Apr 14, 2017 at 23:52
  • $\begingroup$ No. I'm running v. 10.4. $\endgroup$ Commented Apr 15, 2017 at 0:32
2
$\begingroup$

You can use the option ColorFunction (as suggested by swish in the comments) or Style each data point with a color that depends on its Rescaled value:

SeedRandom[123]
d = RandomInteger[10, 10];

Row[{BarChart[d, ColorFunction -> Hue, ImageSize -> 400],
  BarChart[Style[#, Hue[Rescale[#, MinMax @ d]]] & /@ d, ImageSize -> 400]}]

enter image description here

If you use a named color gradient like "Rainbow" you can change the range of the color gradient to MinMax @ d using ColorData[{"Rainbow", MinMax @ d}]:

Row[{BarChart[d, ColorFunction -> "Rainbow", ImageSize -> 400], 
  BarChart[Style[#, ColorData[{"Rainbow", MinMax @ d}]@#] & /@ d, ImageSize -> 400]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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