7

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.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Mr Alpha
  • 1,813
  • 1
  • 16
  • 26
  • Possibly related: [Not cropping BarChart when using Frame instead of Axes](http://stackoverflow.com/questions/6557883/not-cropping-barchart-when-using-frame-instead-of-axes) – Szabolcs Jan 12 '12 at 19:36
  • 1
    It's a rough workaround, but if nothing better comes up, you can do `BarChart[Reverse[data /. x_ /; x == 0 -> 10^-5], ...` (i.e. replace zeros with small numbers just before plotting). I used the pattern `x_ /; x == 0` to match both 0 and 0.0 ... I guess 0|0.0 would have been good as well. – Szabolcs Jan 12 '12 at 19:41

3 Answers3

6

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

Lou
  • 322
  • 4
  • 12
3

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 support@wolfram.com.

Brett Champion
  • 8,497
  • 1
  • 27
  • 44
  • I realize I made a mistake when I edited a sample dataset and plot into the question: the dataset might be real numbers, so `0|0.0 -> 0.00001` or something similar would be better. – Szabolcs Jan 12 '12 at 20:29
  • @Szabolcs Good point. I added your approach (after briefly considering `PossibleZeroQ`...) – Brett Champion Jan 12 '12 at 20:37
2

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

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125