5
$\begingroup$

I'm trying to visualize some data using a BarChart[] in Mathematica.

The data should be placed (centered) inside the bars - except the bar is too small. In this case the date should be written above the bar.

My command for creating the BarChart:

BarChart[{
  {0.123, 0.492}, {2.865, 0.055},
  {1.03, 1.084}, {4.282, 0.053}
  }, AxesLabel -> {"", "Value"},
 ChartLabels -> {
   Placed[{
     "data1", "data2", 
     "data3", "data4"
     }, {{0.5, 0}, {0.8, 1.2}}, Rotate[#, (1.75/7) Pi] &], 
   Placed[{"", ""}, Above]}, 
 LabelingFunction -> (Placed[#1, {{0.5, 0.5}, {0.5, 0.5}}, 
     Rotate[#, 0.5 Pi] &] &), 
 ChartLegends -> {"⌀", "σ"}]

and the result: enter image description here

As you can see the 1st., 4th and 8th value are even below the x-axis.

Is there some way to add some kind of 'exception' for this kind of bars?

$\endgroup$
  • $\begingroup$ If you just want to modify bar charts occasionally, you can just double click on a label and move it. $\endgroup$ – Sjoerd C. de Vries Jul 23 '15 at 12:26
6
$\begingroup$

A small change to the LabelingFunction seems to do the trick:

BarChart[{{0.123, 0.492}, {2.865, 0.055}, {1.03, 1.084}, {4.282, 0.053}},
   AxesLabel -> {"", "Value"}, 
   ChartLabels -> {Placed[{"data1", "data2", "data3", "data4"},
     {{0.5, 0}, {0.8, 1.2}}, Rotate[#, (1.75/7) Pi] &], 
     Placed[{"", ""}, Above]}, 
   LabelingFunction -> (
     Placed[Rotate[#, 0.5 Pi], If[#1 > 0.5, Center, Above]] &),
   ChartLegends -> {"⌀", "σ"}
]

enter image description here

For now, the term 0.5 (the smallest bar size that can have a label inside) needs to be set manually.

$\endgroup$
  • $\begingroup$ I made a couple of changes; hope you don't mind! $\endgroup$ – Mr.Wizard Jul 23 '15 at 13:30
  • $\begingroup$ Exactly what I was looking for. Thank you very much. $\endgroup$ – vigri Jul 23 '15 at 13:34
  • $\begingroup$ @Mr.Wizard Fine. It's clearer this way at the cost of not being able to fine tune the positioning. $\endgroup$ – Sjoerd C. de Vries Jul 23 '15 at 15:31

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.