6
$\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$
1
  • $\begingroup$ If you just want to modify bar charts occasionally, you can just double click on a label and move it. $\endgroup$ Jul 23, 2015 at 12:26

1 Answer 1

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$
3
  • $\begingroup$ I made a couple of changes; hope you don't mind! $\endgroup$
    – Mr.Wizard
    Jul 23, 2015 at 13:30
  • $\begingroup$ Exactly what I was looking for. Thank you very much. $\endgroup$
    – vigri
    Jul 23, 2015 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$ Jul 23, 2015 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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