9
$\begingroup$

In version 11.0.1 it appears that ColorFunction is being ignored for BarChart with grouped data.

With

dat = <|0 -> <|2007 -> 7, 2008 -> 8, 2009 -> 9, 2010 -> 10|>, 
        1 -> <|2007 -> 5, 2008 -> 6, 2009 -> 7, 2010 -> 8|>|>;

Then

BarChart[dat,
 ChartLabels -> {Automatic, None},
 ChartLayout -> "Stacked",
 ChartLegends -> Automatic,
 ColorFunction -> ColorData["DeepSeaColors"]]

Mathematica graphics

All of the 2010 entries are not the same colour and the colours in the legend are not using the colours of ColorFunction. Similarly for the other groups.

This seems like a bug. Any ideas for workarounds? Win 7 Pro

Confirmed bug: Have to figure out the case number as I am getting a couple of confirms at the moment but all on the same case number. It is one of these but three where responded with separate emails all from one case number so I can't tell which one it is. [CASE:3780234] [CASE:3780235] [CASE:3780239]

$\endgroup$
  • $\begingroup$ a workaround: ChartStyle->"DeepSeaColors" instead of ColorFunction? $\endgroup$ – kglr Nov 25 '16 at 14:17
  • $\begingroup$ @kglr I need ColorData as I am actually using ColorData[{"DeepSeaColors", "Reverse"}]. I took that part out to make the question minimal. $\endgroup$ – Edmund Nov 25 '16 at 14:23
  • $\begingroup$ It does seem like a bug, but isn't the coloring in general wrong here? None of the colors for the different years match each other. If you take out the ColorFunction option, then the bars for the years are the same color. $\endgroup$ – Jason B. Nov 25 '16 at 19:32
  • $\begingroup$ @JasonB Yes. I mention that in the question. $\endgroup$ – Edmund Nov 25 '16 at 21:58
  • $\begingroup$ @Edmund, sorry - I didn't read fully and thought your only issue was with the legend colors $\endgroup$ – Jason B. Nov 29 '16 at 14:44
5
$\begingroup$

If you don't mind a crude hack:

Unprotect[ColorData]; 
ColorData["x"] = ColorData[{"DeepSeaColors", "Reverse"}];

BarChart[dat, ChartLabels -> {Automatic, None}, 
 ChartLayout -> "Stacked", ChartLegends -> Automatic, 
 ChartStyle -> "x"]

enter image description here

$\endgroup$
  • $\begingroup$ Interesting workaround. (+1) $\endgroup$ – Edmund Nov 29 '16 at 13:31
4
$\begingroup$

This example is a bit tricky, since you don't actually want to use a ColorFunction, since then the bars are colored by their height and you don't have a consistent coloring by keys.

So the bar chart in the OP looks correct based on the input, but the legend is obviously nonsense - that should be reported as a bug. Perhaps if a ColorFunction is used, the ChartLegend should default to a color bar like DensityPlot gives.

Simon gives a good workaround that uses the ChartStyle -> <named_color_scheme> option method, but as he says it is a bit of a hack (not that I don't love a good hack). This is a convenience method that makes it so you don't have to give explicit styles for each element. But the in this case the OP wants to customize it a bit, having the colors go in reverse order, so we can just use the more basic functionality of ChartStyle and give it a list of Style specifications.

colors = ColorData[{"DeepSeaColors", "Reverse"}] /@ Subdivide[3];
BarChart[dat, ChartLabels -> {Automatic, None}, 
 ChartLayout -> "Stacked", ChartLegends -> Automatic, 
 ChartStyle -> colors]

Mathematica graphics

$\endgroup$
  • $\begingroup$ This is the magic sauce! Exactly what I am looking for and without the hacks. +1 while we wait for challengers. It also appears that I may have misinterpreted the use of ColorFunction in this instance. Perhaps compounded by the odd legend. $\endgroup$ – Edmund Nov 29 '16 at 15:24
1
$\begingroup$

You can also use a color function of your choice as the setting for Method suboption "ChartDefaultStyle":

BarChart[dat,
  ChartLabels -> {Automatic, None},
  ChartLayout -> "Stacked",
  ChartLegends -> Automatic,
  Method -> {"ChartDefaultStyle" -> (ColorData[{"DeepSeaColors", "Reverse"} , #] &)}]

enter image description here

With

dat2 = AssociationThread[Range[0, 5] -> 
 Table[AssociationThread[Range[2000, 2008] -> RandomInteger[9, 9]], 6]]

we get

enter image description here

$\endgroup$
0
$\begingroup$

ChartLegend bug also with PieChart

I give another solution:

I use other data for simplicity, answer is already accepted and I post because I think it can be a simpler approach using Legended[]

Legended[

 PieChart[{mix, males, females},
  ColorFunction -> (Switch[#, mix, LightGreen, males, LightBlue, 
      females, LightPink] &),
  ColorFunctionScaling -> False
  (*,ChartLegends\[Rule]{"mixed gender","male gender",
  "female gender"} BUG*)
  ]
 ,
 PointLegend[{LightGreen, LightBlue, LightPink},
  {"Mix", "Male", "Female"}
  ]
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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