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 have some data which I want to plot in a simple bar chart:

example = {{"Knowledge", 2}, {"Comprehension", 4}, {"Application", 0}, {"Analysis", 1}, {"Synthesis", 0}, {"Evaluation", 2}}

The first element of each pair should be the bar label, and the second should be the height of the bar. I can get the desired result using:

BarChart[Last[#] & /@ example, ChartLabels -> {"Knowledge","Comprehension","Application","Analysis","Synthesis","Evaluation"}]

enter image description here

But my real question is this:

Though

First[#] &/@ example

Returns

{"Knowledge", "Comprehension", "Application", "Analysis", "Synthesis", "Evaluation"}

When I try:

BarChart[Last[#]&/@example, ChartLabels->First[#]&/@example]

I get a graph without labels:

enter image description here

This seems like strange behaviour, can anyone shed some light, or suggest a fix?

share|improve this question
2  
Take a look at what ChartLabels -> First[#] & /@ example is doing. You need a bracket around the second part: ChartLabels -> (First[#] & /@ example) –  Jonathan Shock Jun 7 '13 at 9:08
    
Well, that was easy! Sorry for the wasted time. If you add that as an answer I'll accept it. –  nickjamesuk Jun 7 '13 at 9:10
1  
Much better: BarChart[Last /@ example, ChartLabels -> First /@ example] or BarChart[example[[All, 2]], ChartLabels -> example[[All, 1]]]. –  J. M. Jun 7 '13 at 9:10
    
That is better, thanks for the input. –  nickjamesuk Jun 7 '13 at 9:14
add comment

1 Answer

up vote 4 down vote accepted

You have to be careful with passing in arguments to a function when you have a replacement. The problem is currently that:

ChartLabels -> First[#] & /@ example

is passing the elements of example into all of ChartLabels -> First[#] one by one. You need instead to be passing it only into the second argument and so all that is needed to solve the problem is a set of brackets:

ChartLabels -> (First[#] & /@ example)
share|improve this answer
2  
If you take a look at FullForm[HoldForm[ChartLabels -> First[#] & /@ example]], you can instantly see what exactly is going wrong. –  J. M. Jun 7 '13 at 9:26
add comment

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.