Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm trying to control the color of individual bars in a BarChartwhen I pass a 2 dimensional list of data to plot. What I want to accomplish is that for each pair of adjacent bars I want to use a ColorScheme overall, however, make the right bar to be slightly Darker (see the last image for an example). No color is repeated.

I've tried various combinations of {..., {None}, etc...}, however, I can't figure out how to do what I want without flattening the list.

BarChart[RandomReal[1, {5, 2}], ChartStyle -> "Pastel"]

Mathematica graphics

BarChart[RandomReal[1, {5, 2}], ChartStyle -> {"Pastel", None}]

Mathematica graphics

I thought these might work:

colorfunc = ColorData["Pastel"];
colors = colorfunc /@ Range[0, 1, 1/4];
BarChart[RandomReal[1, {5, 2}], 
 ChartStyle -> {{colors, Darker/@colors}, None}]

but it returns an error.

BarChart[RandomReal[1, {5, 2}], ChartStyle -> {Riffle[colors, Darker[#, 0.1] & /@ colors]}]

Mathematica graphics

As I said I can accomplish what I want by flattening the list but this breaks the nice bar spacing and a lot of the automated features of the plotting.

BarChart[Flatten@RandomReal[1, {5, 2}], ChartStyle -> Riffle[colors, Darker[#, 0.1] & /@ colors]]

Mathematica graphics

How can I control specific specific bars in a BarChart with multi-dimensional data?

share|improve this question

marked as duplicate by Mr.Wizard Feb 24 '13 at 10:38

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.

Is BarChart[RandomReal[1, {5, 2}], ChartStyle -> {None, {Blue, Darker[Blue]}}] what you want? – Silvia Feb 24 '13 at 4:05
@Silvia, unfortunately no. I edited my question to be more clear but I want to follow the ColorSheme overall for the left bar and then the right bar of each pair will be simply slightly darker. No color repeats. – s0rce Feb 24 '13 at 4:26
How about this: BarChart[RandomReal[1, {5, 2}], ChartStyle -> {"Pastel", {Opacity[.6], Opacity[1]}}]? – Silvia Feb 24 '13 at 4:44
related Q/A: Styling individual bars in a BarChart with grouped data – kglr Feb 24 '13 at 8:45
I closed this as a duplicate. If you disagree please vote to reopen and @ping me with reasons why. – Mr.Wizard Feb 24 '13 at 10:39

From what the documentation says, ChartStyle accepts no functions but only styles, so if it's acceptable for you, Opacity might be a possible replacement:

BarChart[RandomReal[1, {5, 2}], 
   ChartStyle -> {"Pastel", {Opacity[.234], Opacity[1]}}]

Mathematica graphics

And you can process to what you want from the above one:

% /. {Directive[pre___, RGBColor[rgb__], Opacity[op_], post___] :> 
   Directive[pre, If[op - .234 == 0, RGBColor[rgb], Darker[RGBColor[rgb]]], post]}

Mathematica graphics

share|improve this answer
I was writing a comment that the Opacity causes the GridLines to show through half the bars but with your post-processing strategy the problem is solved! – s0rce Feb 24 '13 at 5:18
@s0rce Always have fun with post-processing :) – Silvia Feb 24 '13 at 5:19
Clever approach. +1 – Mr.Wizard Feb 24 '13 at 6:25
@Mr.Wizard Thanks~ – Silvia Feb 24 '13 at 6:37
I believe your replacement rule may be simplified. May I make the edit? – Mr.Wizard Feb 24 '13 at 6:37

This is a tangential answer.

Silvia's answer shows a nice and clever method but it is somewhat complicated by the replacement rule needed for post-processing. This made me realize that a new kind of directive would be convenient here, one which could be used in-line. For example: {. . ., Red, . . ., mkDarker[], . . .} would replace mkDarker[] with Darker@Red. First an illustration of the directive in use, using Silvia's method (I'll use Glow instead of Opacity just because I can):

ch = BarChart[RandomReal[1, {5, 2}], ChartStyle -> {"Pastel", {Glow[], Glow[0]}}];
ch /. Glow[0] :> mkDarker[]

Mathematica graphics

Or darker by a different amount:

ch /. Glow[0] :> mkDarker[0.6]

Mathematica graphics

And here is the code for the directive and mkLighter too while we're at it:

mkDarker /: (h : List | Directive)[Longest[a___], 
  color : _RGBColor | _Hue | _CMYKColor | _GrayLevel, b___, 
  mkDarker[n : (_?NumericQ) : 1/3], c___] := h[a, color, b, Darker[color, n], c]

mkLighter /: (h : List | Directive)[Longest[a___], 
  color : _RGBColor | _Hue | _CMYKColor | _GrayLevel, b___, 
  mkLighter[n : (_?NumericQ) : 1/3], c___] := h[a, color, b, Lighter[color, n], c]
share|improve this answer
Great +1! This is what it should be! I thought about similar things but gave up mainly because TagSetDelayed didn't come to my mind. – Silvia Feb 24 '13 at 7:25
Would it be possible to use mkDarker directly in ChartStyle? Or maybe it will require a hack of option-check process of BarChart? – Silvia Feb 24 '13 at 7:30
@Silvia I think it would require some kind of modification to BarChart and I thought it was best to avoid that, given how simple the post-processing rule can be when using this. – Mr.Wizard Feb 24 '13 at 7:34
I traced to a function Charting`generalDirective, setting it to Charting`generalDirective[mkDarker[_?NumericQ]] := True, than it's OK to use BarChart[RandomReal[1, {5, 2}], ChartStyle -> {"Pastel", {mkDarker[0], mkDarker[.4]}}]. It looks pretty safe to me. – Silvia Feb 24 '13 at 9:51
@Silvia That sounds very useful. You dug that out yourself? – Mr.Wizard Feb 24 '13 at 11:37

Using the same tricks in this answer to a related Q/A:

 data = RandomReal[1, {5, 2}]; 
 styles = {#, Darker[#, .1]} & /@ colors;
 BarChart[MapThread[Style[#1, #2] &, {data, styles}, 2], BarSpacing -> {0, 0.4}]


 BarChart[ Partition[Inner[Style, Flatten@data, Flatten@styles, List], 2], 
 BarSpacing -> {0, 0.4}]

or, for Version 9,

  BarChart[ ArrayReshape[Inner[Style, Flatten@data, Flatten@styles, List], {5, 2}],
  BarSpacing -> {0, 0.4}]

enter image description here

share|improve this answer
I missed that earlier question. To me this one appears to be a duplicate. Do you disagree? – Mr.Wizard Feb 24 '13 at 10:30
@Mr.Wizard, I agree; this is a simpler version of the linked question. – kglr Feb 24 '13 at 10:37

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