Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 553
Mr Alpha Avatar asked Jan 12 '12 19:01

Mr Alpha


3 Answers

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

like image 198
Lou Avatar answered Nov 20 '22 01:11

Lou


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].

like image 36
Brett Champion Avatar answered Nov 20 '22 00:11

Brett Champion


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

like image 2
Mr.Wizard Avatar answered Nov 20 '22 02:11

Mr.Wizard