TransWikia.com

Plotting Error bar on BarChart

Mathematica Asked by ljsg on August 11, 2021

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.

One Answer

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

Correct answer by kglr on August 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP