How to stop barchart from hiding labels for data value 0 in Mathematica?

1.1k views Asked by At

I use this to create a Bar Chart:

BarChart[
 Reverse@data,
 BarOrigin -> Left,
 ChartLabels -> 
  Placed[{Reverse@labels, Reverse@data}, {Before, After}],
 ChartElementFunction -> "FadingRectangle"
 ]

With data = {7, 10, 0, 6, 0, 3, 5} this gives

Mathematica graphics

The problem is that some of the data values are 0 and BarChart won't even add labels for them. Instead it leaves a open space. How can I get it to still add the labels even though the values are 0?

This is with Mathematica 8.

3

There are 3 answers

1
Lou On BEST ANSWER

What about

data = {7, 10, 0, 6, 0, 3, 5}

labels = ("label " ~~ ToString[#]) & /@ data

BarChart[Reverse@data, BarOrigin -> Left,
ChartLabels -> Placed[{Reverse@labels, Reverse@data}, {Axis, After}],
ChartElementFunction -> "FadingRectangle"]

It seems that "Before" doesn't and "Axis" does work?

chart

1
Mr.Wizard On

Your code works as given in Mathematica 7 on Windows 7.

data = {7, 10, 0, 6, 0, 3, 5};

labels = Row[{"label",#}]& /@ data;

BarChart[
  Reverse@data,
  BarOrigin -> Left,
  ChartLabels ->
   Placed[{Reverse@labels, Reverse@data}, {Before, After}],
  ChartElementFunction -> "FadingRectangle"
]

Mathematica graphics

2
Brett Champion On

The simplest approach is to use a hack like data /. {(0|0.0) -> 0.00001}.

I think this should work without the need for a hack, so you should also file a report with [email protected].