# ChartLegends and BarChart Ignores ColorFunction

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"]]


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]

• a workaround: ChartStyle->"DeepSeaColors" instead of ColorFunction?
– kglr
Nov 25, 2016 at 14:17
• @kglr I need ColorData as I am actually using ColorData[{"DeepSeaColors", "Reverse"}]. I took that part out to make the question minimal. Nov 25, 2016 at 14:23
• 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. Nov 25, 2016 at 19:32
• @JasonB Yes. I mention that in the question. Nov 25, 2016 at 21:58
• @Edmund, sorry - I didn't read fully and thought your only issue was with the legend colors Nov 29, 2016 at 14:44

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"]


• Interesting workaround. (+1) Nov 29, 2016 at 13:31

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]


• 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. Nov 29, 2016 at 15:24

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"} , #] &)}]


With

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


we get

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"}
]
]