Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am using version 8 and I have the following code:

totals4 = {{1093, "AUSTRALIA"}, {235, "BELGIUM"}, {474, "BERMUDA"}, {187, "BRAZIL"}};
BarChart[#, ChartStyle -> "Pastel", 
         ChartLabels -> (Placed[Rotate[Style[#, Blue], Pi/3] & /@ #2, {{.5, 0}, {1, 1}}])
         ] & @@ Transpose[totals4]

Is there a way to get the labels to be the same color as their respective bar?

Thank you in advance.

bar chart

share|improve this question
    
Where is totals4? –  J. M. Jun 4 '13 at 1:02
    
totals4 has this data: {{1093, "AUSTRALIA"}, {235, "BELGIUM"}, {474, "BERMUDA"}, {187, "BRAZIL"}} –  sente Jun 4 '13 at 1:39
    
At this point I'd like to register a small protest on behalf of people with less than perfect eyesight who will find rotated pastel-colored text difficult to read. Why not make the labels simple, straight, and readable, rather than 'interesting'?! OK, protest over... :) –  cormullion Jun 4 '13 at 6:58
    
If it helps, the chart will be going on a website with a gray background. –  sente Jun 4 '13 at 9:39
1  
@sente Nooooooo!!!!!! :) well, ok, but run it through the accessibility checker first. Or read web pages that suck... :) –  cormullion Jun 4 '13 at 16:14

2 Answers 2

up vote 7 down vote accepted

I guess something like this:

BarChart[Range[4], ChartStyle -> "Pastel", ChartLabels ->
         MapIndexed[Style[#1, ColorData["Pastel", Rescale[#2[[1]], {1, 4}]], Bold, 16] &,
                    CharacterRange["A", "D"]]]

bar chart with colored labels

Since ColorData["Pastel"] only supports arguments in the interval $[0,1]$, a rescaling is necessary; ponder for instance on the result of Rescale[Range[4], {1, 4}].

share|improve this answer
    
The name of the countries will change depending upon the query. Is it possible to not hard code it? In other words Australia is Purple and Belgium would be Orange and so on. If it is not possible with labels is it possible with legends? –  sente Jun 4 '13 at 1:44
    
Well, some legwork from you, first: did you already manage to produce a bar chart with the bars colored the way you want them? (That is, data for Australia is purple, etc.) –  J. M. Jun 4 '13 at 1:46
    
Well the query that generated the data was this one: totals4 = SQLExecute[conn, " SELECT t2.cnt, t2.description FROM name_loc t1 INNER JOIN ( SELECT COUNT() 'cnt', f.fk_cnty_id 'fk_cnty_id', c.description \ 'description' FROM name_loc f INNER JOIN countries c ON f.fk_cnty_id = c.id and c.letter_code <> 'US' INNER JOIN identifiers i ON f.fk_ident_id = i.id GROUP BY f.fk_cnty_id HAVING COUNT()>100 ) as t2 ON t1.fk_cnty_id = t2.fk_cnty_id GROUP BY t1.fk_cnty_id limit 4 "] –  sente Jun 4 '13 at 2:13
    
The exact color for the country does not matter. Usually there are thirty countries in the chart, that is why hard coding it is not an option. Basically I want to take whatever color Mathematica assigns to a particular bar and then use the same color in the label. If this is possible it would be great. –  sente Jun 4 '13 at 2:17
    
Well, all you really need is the length of your list of countries, or whatever. At the bare minimum, you can do With[{n = Length[countries]}, BarChart[data, ChartStyle -> "Pastel", ChartLabels -> MapIndexed[Style[#1, ColorData["Pastel", Rescale[#2[[1]], {1, n}]]] &, countries]]]. I gather it was not immediately apparent to you that the Range[4] and CharacterRange["A", "D"] were replaceable with your data; you did after all not initially mention the data you're working with. –  J. M. Jun 4 '13 at 2:25

For Version 7

In version 7 the underlying Graphics expression that is produced by BarChart is highly conducive to a post-processing approach.

  • Each bar and label is contained in an expression that also includes style information.

  • The label however has an additional Style wrapper to effectively reset the color.

  • To style the bars and labels similarly we need only to remove this undesired re-styling.

This can be accomplished with the following replacement rule:

 Style[obj_, {GrayLevel[0]}] :> obj

Complete:

totals4 = {{1093, "AUSTRALIA"}, {235, "BELGIUM"}, {474, "BERMUDA"}, {187, "BRAZIL"}};

BarChart[#, ChartStyle -> "Pastel", 
    ChartLabels -> (Placed[Rotate[#, Pi/3] & /@ #2, {{.5, 0}, {1, 1}}])] & @@ 
  Transpose[totals4] /. Style[obj_, {GrayLevel[0]}] :> obj

enter image description here

The rule may need to be made more specific if you have other objects with an explicit style of {GrayLevel[0]} in the chart. An example of a more restrictive pattern that might be needed is:

Text[Style[obj_, {GrayLevel[0]}], pos__] :> Text[obj, pos]
share|improve this answer
    
Maybe one could also suggest using a background that has better contrast with the pastels... Perhaps just use Framed on the labels and insert a background color there. Anyway, nice observation of the inner structure. –  Jens Jun 4 '13 at 4:20
    
@Jens can you confirm that this works on versions other than 7? –  Mr.Wizard Jun 4 '13 at 4:22
    
Oh wait, it doesn't work on version 8. I forgot your version 7 comment... Maybe wrap the labels in Tooltip and use a pattern based on that. I may give it a shot. –  Jens Jun 4 '13 at 4:24
1  
Unfortunately, in version 8, the text labels are no longer grouped in the same list as the bar they belong to. So the solution by that new guy @0x4A4D may be the best bet. –  Jens Jun 4 '13 at 4:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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