2
$\begingroup$

May I ask how to add error bar into the bar chart? The standard deviation and the mean are included in the below code.

JoinMean = Join[{MeanC5cm, SMeanC5cm}]
 {44.0215, 58.2535}
JoinStDv = Join[{Sd5, SSd5}]
 {3.31495, 3.72921}
 label = {"unstretch", "Stretch"};

BarChart[{JoinMean}, PlotLabel -> "5cm", 
 AxesLabel -> {"", "KΩ"}, 
 ChartLabels -> Placed[label, Axis]]

Thank you.

$\endgroup$

1 Answer 1

2
$\begingroup$

Attach the standard deviations to main data as metadata and use a custom ChartElementFunction that combines built-in ChartElementFunctions with error lines using the information passed as metadata:

ClearAll[errorLines]
errorLines[cedf__: "Rectangle"][t_: .5, style_: Thick] := 
  Module[{a = Mean[#[[1]]], b = -Subtract @@ #[[1]], c = #[[2, 2]], d = #3[[1]], 
     color = Darker @ Charting`ChartStyleInformation["Color"]}, 
   {ChartElementData[cedf][##], color, style, Line[{{a, c - d}, {a, c + d}}],
     Line[Table[{a + i t b/2, c + j  d}, {j, {-1, 1}}, {i, {-1, 1}}]]}] &;

Examples:

JoinMean = {44.0215, 58.2535};
JoinStDv = {3.31495, 3.72921};

BarChart[Thread[JoinMean -> JoinStDv], PlotLabel -> "5cm", 
 AxesLabel -> {"", "KΩ"}, ChartStyle -> "Pastel", 
 ChartLabels -> Placed[label, Axis], ImageSize -> 400, 
 ChartElementFunction -> errorLines[][]]

enter image description here

Change the option setting for ChartElementFunction to

errorLines["GradientScaleRectangle", "ColorScheme" -> "Rainbow"][.8, Thickness[.02]]

to get

enter image description here

$\endgroup$
3
  • $\begingroup$ May i ask what is the first line of code trying to do? $\endgroup$
    – ljsg
    Commented Jun 27, 2018 at 14:12
  • $\begingroup$ @junliang04 it removes any store values and options associated with the symbol errorlines. It is good practice to do so before making a delayed assignment, like the line that follows. Have you had a chance to look up ClearAll in the documentation? $\endgroup$
    – MarcoB
    Commented Jun 27, 2018 at 14:20
  • $\begingroup$ Thank you so much. $\endgroup$
    – ljsg
    Commented Jun 27, 2018 at 14:24

Your Answer

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

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