Skip to main content
added 1311 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959

Update: to make it work with non-default BarOrigin settings:

ClearAll[ceF] ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{bo = Charting`ChartStyleInformation["BarOrigin"], col = Darker[Charting`ChartStyleInformation["Color"]], box = #, tf, e}, tf = Switch[bo, Left | Right, Reverse, _, Identity]; box = Switch[bo, Bottom, box, Top, {box[[1]], Reverse[box[[2]]]}, Left, Reverse@box, Right, {box[[2]], Reverse@box[[1]]}]; e = nsd /2 Sqrt[Abs@box[[2, 2]] (1 - Abs@box[[2, 2]]/#3[[1]])]; {ChartElementData[cedf][##], Thick, color /. Automatic -> col, Line[tf /@ {{Mean@box[[1]], box[[2, 2]] - e}, {Mean@box[[1]], box[[2, 2]] + e}}], Line[tf /@ {{box[[1, 1]] + d/2, box[[2, 2]] - e}, {box[[1, 2]] - d/2, box[[2, 2]] - e}}], Line[tf /@ {{box[[1, 1]] + d/2, box[[2, 2]] + e}, {box[[1, 2]] - d/2, box[[2, 2]] + e}}]}] & 

Example:

Grid[Partition[Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[][], ImageSize -> 300, BarOrigin -> #] & /@ {Bottom, Top, Left, Right}, 2], Dividers -> All] 

enter image description here

Update: to make it work with non-default BarOrigin settings:

ClearAll[ceF] ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{bo = Charting`ChartStyleInformation["BarOrigin"], col = Darker[Charting`ChartStyleInformation["Color"]], box = #, tf, e}, tf = Switch[bo, Left | Right, Reverse, _, Identity]; box = Switch[bo, Bottom, box, Top, {box[[1]], Reverse[box[[2]]]}, Left, Reverse@box, Right, {box[[2]], Reverse@box[[1]]}]; e = nsd /2 Sqrt[Abs@box[[2, 2]] (1 - Abs@box[[2, 2]]/#3[[1]])]; {ChartElementData[cedf][##], Thick, color /. Automatic -> col, Line[tf /@ {{Mean@box[[1]], box[[2, 2]] - e}, {Mean@box[[1]], box[[2, 2]] + e}}], Line[tf /@ {{box[[1, 1]] + d/2, box[[2, 2]] - e}, {box[[1, 2]] - d/2, box[[2, 2]] - e}}], Line[tf /@ {{box[[1, 1]] + d/2, box[[2, 2]] + e}, {box[[1, 2]] - d/2, box[[2, 2]] + e}}]}] & 

Example:

Grid[Partition[Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[][], ImageSize -> 300, BarOrigin -> #] & /@ {Bottom, Top, Left, Right}, 2], Dividers -> All] 

enter image description here

deleted 2 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959

For the case where the height function is "Count", usingwe can use the formula from the linked page in a custom ChartElementFunction with the size of the sample size (Length[data]) passed as metadata:

ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, color /. Automatic -> Darker[Charting`ChartStyleInformation["Color"]], Line[{{Mean@#[[1]], #[[2, 2]] - e}, {Mean@#[[1]], #[[2, 2]] + e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] - e}, {#[[1, 2]] - d/2, #[[2, 2]] - e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] + e}, {#[[1, 2]] - d/2, #[[2, 2]] + e}}]}]& 

Examples:

SeedRandom[1] data = RandomVariate[NormalDistribution[0, 1], 200]; Histogram[data -> Length[data], ChartStyle -> 43, ChartElementFunction -> ceF[][]] 

enter image description here

Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[.2, 3, Black]["GlassRectangle"]] 

enter image description here

For the case where the height function is "Count", using the formula from the linked page in a custom ChartElementFunction with the size of the sample (Length[data]) passed as metadata:

ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, color /. Automatic -> Darker[Charting`ChartStyleInformation["Color"]], Line[{{Mean@#[[1]], #[[2, 2]] - e}, {Mean@#[[1]], #[[2, 2]] + e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] - e}, {#[[1, 2]] - d/2, #[[2, 2]] - e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] + e}, {#[[1, 2]] - d/2, #[[2, 2]] + e}}]}]& 

Examples:

SeedRandom[1] data = RandomVariate[NormalDistribution[0, 1], 200]; Histogram[data -> Length[data], ChartStyle -> 43, ChartElementFunction -> ceF[][]] 

enter image description here

Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[.2, 3, Black]["GlassRectangle"]] 

enter image description here

For the case where the height function is "Count", we can use the formula from the linked page in a custom ChartElementFunction with the sample size (Length[data]) passed as metadata:

ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, color /. Automatic -> Darker[Charting`ChartStyleInformation["Color"]], Line[{{Mean@#[[1]], #[[2, 2]] - e}, {Mean@#[[1]], #[[2, 2]] + e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] - e}, {#[[1, 2]] - d/2, #[[2, 2]] - e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] + e}, {#[[1, 2]] - d/2, #[[2, 2]] + e}}]}]& 

Examples:

SeedRandom[1] data = RandomVariate[NormalDistribution[0, 1], 200]; Histogram[data -> Length[data], ChartStyle -> 43, ChartElementFunction -> ceF[][]] 

enter image description here

Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[.2, 3, Black]["GlassRectangle"]] 

enter image description here

added 211 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959

For the case where the height function is "Count", using the formula from the linked page in a custom ChartElementFunction with the size of the sample (Length[data]) passed as metadata:

ceF[d_: .2, nsd_: 3][cedf_3, color_: Automatic][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, color /. Automatic -> Darker[Charting`ChartStyleInformation["Color"]], Line[{{Mean@#[[1]], #[[2, 2]] - e}, {Mean@#[[1]], #[[2, 2]] + e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] - e}, {#[[1, 2]] - d/2, #[[2, 2]] - e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] + e}, {#[[1, 2]] - d/2, #[[2, 2]] + e}}]}]& 

Examples:

SeedRandom[1] data = RandomVariate[NormalDistribution[0, 1], 200]; Histogram[data -> Length[data], ChartStyle -> 43, ChartElementFunction -> ceF[][]] 

enter image description here

Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[.2, 3, Black]["GlassRectangle"]] 

enter image description here

For the case where the height function is "Count", using the formula from the linked page in a custom ChartElementFunction with the size of the sample (Length[data]) passed as metadata:

ceF[d_: .2, nsd_: 3][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, Darker[Charting`ChartStyleInformation["Color"]], Line[{{Mean@#[[1]], #[[2, 2]] - e}, {Mean@#[[1]], #[[2, 2]] + e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] - e}, {#[[1, 2]] - d/2, #[[2, 2]] - e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] + e}, {#[[1, 2]] - d/2, #[[2, 2]] + e}}]}]& 

Examples:

SeedRandom[1] data = RandomVariate[NormalDistribution[0, 1], 200]; Histogram[data -> Length[data], ChartStyle -> 43, ChartElementFunction -> ceF[][]] 

enter image description here

For the case where the height function is "Count", using the formula from the linked page in a custom ChartElementFunction with the size of the sample (Length[data]) passed as metadata:

ceF[d_: .2, nsd_: 3, color_: Automatic][cedf_: "Rectangle"] := Module[{e = nsd /2 Sqrt[#[[2, 2]] (1 - #[[2, 2]]/ #3[[1]])]}, {ChartElementData[cedf][##], Thick, color /. Automatic -> Darker[Charting`ChartStyleInformation["Color"]], Line[{{Mean@#[[1]], #[[2, 2]] - e}, {Mean@#[[1]], #[[2, 2]] + e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] - e}, {#[[1, 2]] - d/2, #[[2, 2]] - e}}], Line[{{#[[1, 1]] + d/2, #[[2, 2]] + e}, {#[[1, 2]] - d/2, #[[2, 2]] + e}}]}]& 

Examples:

SeedRandom[1] data = RandomVariate[NormalDistribution[0, 1], 200]; Histogram[data -> Length[data], ChartStyle -> 43, ChartElementFunction -> ceF[][]] 

enter image description here

Histogram[data -> Length@data, ChartStyle -> 43, ChartElementFunction -> ceF[.2, 3, Black]["GlassRectangle"]] 

enter image description here

Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
Loading