Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

ErrorBars.m

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4427] ErrorBars.m
  • From: fast at newton.Colorado.EDU (Bruce Alan Fast)
  • Date: Mon, 22 Jul 1996 00:59:38 -0400
  • Organization: University of Colorado at Boulder
  • Sender: owner-wri-mathgroup at wolfram.com

mharm at gizmo.usc.edu (Mike Harm) wrote:
 >> I'm using the Graphics package to do some bar charts.  
 >> I've figured out how to do it, but how does one add
 >> error bars to the bars, to indicate statistical standard
 >> error?  Any insights?

dsmith at psy.ucsd.edu wrote:
 > Maybe there is a way of combining ErrorListPlot with BarChart (if
 > anyone knows?)


 Locally we've used this package "ErrorBars.m", which uses more
 primitive graphics commands ("Rectangle") to make error bars,
 rather than building on BarChart.  It was actually made to provide
 'caps' to the bars, and to show both delta-x and delta-y errors.
 We made it work on *our* data, maybe it will work for you...


(* ********************************************************************* *)
(* ** file "ErrorBars.m" ***********2/20/96 Bruce.Fast at Colorado.EDU***** *)
(* ********************************************************************* *)
(* *******  see examples of usage at end of this file .... ************* *)
(* ********************************************************************* *)
BeginPackage["ErrorBars`"];

ErrorBars::usage =
"ErrorBars[{{y1, dy1}, {y2, dy2}, ...}] plots a list of data with error bars.
 ErrorBars[{{x1, y1, dy1}, ...}] allows x, as well as y, positions to
	be specified.
 ErrorBars[{{x1, y1, dx1, dy1}, ...}] allows for horizontal (x) error bars
	in addition to the traditional vertical error bars.
In addition to the usual Graphics options there are four more,
with default values PointSz->0.015, BarWidth->0.018, CapWidth->0.022
(all of which are fractional value of window width),
and BarColor->Hue[0.55]";

Options[ErrorBars] = Flatten[{Options[Graphics],
		PointSz ->0.015,
		BarWidth->0.018,
		CapWidth->0.022,
		BarColor->Hue[0.55]
		}];

SetOptions[ErrorBars, Axes -> Automatic, PlotRange -> All];

Begin["`Private`"];

ErrorBars[l4:{{_, _, _, _}..},opts___] := Module[{optl={opts}, ar, rng, r,
		barwid, xbarwid, ybarwid, capwid, xcapwid, ycapwid, pltrng,
		capthk=0.004, xcapthk, ycapthk, xbw, xcw, xct, pntsiz, barcolor},
	barcolor=BarColor /. optl /. Options[ErrorBars];
	pntsiz  =PointSz  /. optl /. Options[ErrorBars];
	barwid  =BarWidth /. optl /. Options[ErrorBars];
	capwid  =CapWidth /. optl /. Options[ErrorBars];
	ar      = N[AspectRatio /. optl /. Options[ErrorBars]];
	optl = Flatten[{optl, Options[ErrorBars]}];
	optl = DeleteCases[optl, BarColor->_];
	optl = DeleteCases[optl, PointSz->_];
	optl = DeleteCases[optl, BarWidth->_];
	optl = DeleteCases[optl, CapWidth->_];
	rng = 0.5(Max[Transpose[l4][[2]]+Abs[Transpose[l4][[4]]]]-
		Min[Transpose[l4][[2]]-Abs[Transpose[l4][[4]]]]);
		If[rng==0,rng=1];
		{ycapthk, xbarwid} = N[rng {capthk, barwid}];
	rng = 0.5(Max[Transpose[l4][[1]]+Abs[Transpose[l4][[3]]]]-
		Min[Transpose[l4][[1]]-Abs[Transpose[l4][[3]]]]);
		If[rng==0,rng=1];
		{xcapthk, ybarwid} = N[ar rng {capthk, barwid}];
		xcapwid = xbarwid capwid / barwid;
		ycapwid = ybarwid capwid / barwid;
       	r = (pntsiz/barwid){ybarwid,xbarwid};
        Show[ Graphics[ Module[ {i, x, y, dx, dy} ,
                Table[ {x, y, dx, dy} = l4[[i]] ;
            {xcw,xct,xbw}=If[dx==0,{0,0,0},{xcapwid,xcapthk,xbarwid}]; {
	{barcolor, Rectangle[{x-dx, y-xbw}, {x+dx, y+xbw}]},
		{Rectangle[{x-dx-xct,y-xcw},{x-dx+xct,y+xcw}]},
		{Rectangle[{x+dx-xct,y-xcw},{x+dx+xct,y+xcw}]},
 	{barcolor, Rectangle[{x-ybarwid, y-dy}, {x+ybarwid, y+dy}]},
		{Rectangle[{x-ycapwid,y-dy-ycapthk},{x+ycapwid,y-dy+ycapthk}]},
		{Rectangle[{x-ycapwid,y+dy-ycapthk},{x+ycapwid,y+dy+ycapthk}]},
       	{GrayLevel[0], Disk[{x, y},r]}
		} , {i, Length[l4]}]
        ]], Sequence @@ optl ]];

ErrorBars[l3:{{_, _, _}..},opts___] := Module[{i},
	ErrorBars[ Table[ Insert[l3[[i]], 0, 3], {i, Length[l3]}], opts] ]

ErrorBars[l2:{{_, _}..},opts___] := Module[ {i},
	ErrorBars[ Table[ Prepend[l2[[i]], i], {i, Length[l2]}], opts] ]

End[ ];
EndPackage[ ];

(* ************************************************** *)
(* ******************* EXAMPLE 1 ******************** *)
(* ************************************************** *)
(*

ErrorBars[Table[ { i,i^2},{i,10}] ];

ErrorBars[Table[ { i,i^2},{i,10}], PlotRange->All];

*)
(* ************************************************** *)
(* ******************* EXAMPLE 2 ******************** *)
(* ************************************************** *)
(*

data = {{1.0, 1.0, 0.15},
	{2.5, 2.7, 0.10},
	{1.0, 2.7, 0.08},
	{2.0, 1.0, 0.21}};

ErrorBars[ data,
		PointSz ->0.025,
		BarWidth->0.018,
		CapWidth->0.022,
		BarColor->Hue[0.23]];

ErrorBars[ data,
		PointSz ->0.025,
		BarWidth->0.018,
		CapWidth->0.022,
		BarColor->Hue[0.23],
		PlotRange->{{0,3},{0,3}}];

ErrorBars[ data,
		PointSz ->{0.027,0.022},
		BarWidth->0.018,
		CapWidth->0.022,
		BarColor->Hue[0.23],
		PlotRange->{{0,3},{0,3}}];

*)
(* ************************************************** *)
(* ******************* EXAMPLE 3 ******************** *)
(* ************************************************** *)
(*
ErrorBars[ Table[ { 8Sin[j Pi/6], 8Cos[j Pi/6], Random[], Random[]},
		{j,12}],
		PlotLabel->"ErrorBars for both dx and dy"
		];

ErrorBars[ Table[ { 8Sin[j Pi/6], 8Cos[j Pi/6], Random[]},
		{j,12}],
		PlotLabel->"ErrorBars for dy only"
		];
*)
(* ************************************************** *)
(* ******************* EXAMPLE 4 ******************** *)
(* ************************************************** *)
(*
ErrorBars[ Table[ { 8Sin[j Pi/6], 8Cos[j Pi/6], Random[], Random[]},{j,12}],
                PointSz ->0.012,
                BarWidth->0.019,
                CapWidth->0.024,
                BarColor->Hue[0.25],
                PlotLabel->"ErrorBars with all the trimmings"
	];

*)
(* ************************************************** *)
(* ******************* EXAMPLE 5 ******************** *)
(* ************************************************** *)
(*

errjunk = {	{0.04,	0.0336667, 0.0001, 0.0011547},
		{0.09,	0.292,     0.0004, 0.0001},
		{0.16,	0.833667,  0.004,  0.00288675},
		{0.26,	1.27867,   0.016,   0.0521455},
		{0.40,	2.83067,   0.0007, 0.0110151},
		{0.60,	3.08467,   0.001,  0.0442869},
		{1.01,	3.16967,   0.0052, 0.0580546}};

ErrorBars[ errjunk, PlotLabel->"only default options" ]


ErrorBars[ errjunk, PointSz->0.00, BarWidth->0.007, CapWidth->0.009,
		PlotLabel->"PointSz->0, BarWidth->0.007, CapWd->0.009"];


ErrorBars[ errjunk, PointSz->0.00, BarWidth->0.007, CapWidth->0.009,
		PlotRange->{{-0.2,1.2},{0,3.2}},
		PlotLabel->"...plus specifying PlotRange"
		];
*)

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: bar charts with error bars?
  • Next by Date: Re: Revealing HiddenSurfaces
  • Previous by thread: bar charts with error bars?
  • Next by thread: Re: Revealing HiddenSurfaces