1
$\begingroup$

I need to plot BarChart with the error bars. What's the most straightforward way to do it?#plot range from 8 to 10 {8.5,8.6,8.9,9.5} and error bars of 0.1 and labeling of a, b, c and d. Is there something like "ErrorBarPlots`" package which I can use for BarChart?

$\endgroup$
  • $\begingroup$ Have you seen this? $\endgroup$ – BlacKow Apr 19 '16 at 15:22
  • $\begingroup$ @BlacKow yes, But for my simple case, there should be a simple way. $\endgroup$ – faf Apr 19 '16 at 15:23
  • $\begingroup$ Your x intervals aren't equal. What about your bars? you want them to have the same width or different width? ListLinePlot with Filling option? $\endgroup$ – BlacKow Apr 19 '16 at 16:22
  • $\begingroup$ @BlacKow x intervals are the same and just labels. $\endgroup$ – faf Apr 19 '16 at 16:42
  • $\begingroup$ ` {8.5,8.6,8.9,9.5}` definitely not the same. Do you need these number represented on your plot? Or it will be just four bars labeled a,b,c,d? $\endgroup$ – BlacKow Apr 19 '16 at 16:45
2
$\begingroup$

I'm not sure how is it simple, but something like this should work. As a bonus you can change bar width.

Needs["ErrorBarPlots`"]
prepData[data_, 
  size_] := {{#2 - size/2, 
      0}, {#2 - size/2, #1}, {#2 + size/2, #1}, {#2 + size/2, 0}} & @@@
    MapIndexed[{#1, First@#2} &, data] // Flatten[#, 1] &

data = {{8.5, 8.6, 8.9, 9.5}, {"a", "b", "c", "d"}};

Show[ListLinePlot[prepData[#1, 0.9], Filling -> Axis, 
    PlotRange -> {Automatic, {8, 10}}, 
    Ticks -> {MapIndexed[{First@#2, #1} &, #2], Automatic}], 
   ErrorListPlot@
    MapIndexed[{{First@#2, #1}, ErrorBar[0.1]} &, #1]] & @@ data

enter image description here

$\endgroup$
  • $\begingroup$ Well done, thanks. $\endgroup$ – faf Apr 19 '16 at 17:27
3
$\begingroup$
data = {{8.5, 8.6, 8.9, 9.5}, {"a", "b", "c", "d"}};
bcdata = # -> RandomReal[.5] & /@ data[[1]];

bc = BarChart[bcdata, ChartLabels -> Placed[data[[2]], Axis],
 ChartElementFunction -> errorBar[],  PlotRange -> {8, 10}, 
 ImageSize -> 500, ImagePadding -> 15]

Mathematica graphics

To crop the bottom of the bars:

modifyF@bc

Mathematica graphics

where the functions errorBar and modifyF are

errorBar[type_: "Rectangle"][{{x0_, x1_}, {y0_, y1_}}, value_, 
  meta_] := Block[{error}, error = Flatten[meta];
  error = If[error === {}, 0, Last[error]];
  {ChartElementData[type][{{x0, x1}, {y0, y1}}, value, meta], {Black, 
    Line[{{{(x0 + x1)/2, y1 - error}, {(x0 + x1)/2, y1 + error}}, 
         {{1/4 (3 x0 + x1), y1 + error}, {1/4 (x0 + 3 x1), y1 + error}}, 
         {{1/4 (3 x0 + x1), y1 - error}, {1/4 (x0 + 3 x1), y1 - error}}}]}}]

modifyF = With[{pl = PlotRange@#}, # /. RectangleBox[{xmin_, 0.}, {xmax_, ymax_}, z___] :> 
      RectangleBox[{xmin, pl[[2, 1]]}, 
       {xmax, Min[pl[[2, 2]], Max[pl[[2, 1]], ymax]]}, z]] &;
$\endgroup$
  • $\begingroup$ This is actually a pretty straightforward solution, once you spend an hour studying it -- typical for MM. It uses BarChart and the ChartElementFunction option exactly for its intended purpose -- as opposed to turning ListLinePlot into a bar chart and using an external package. $\endgroup$ – Chris Nadovich Jul 7 '17 at 16:35

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.