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
?
$\begingroup$
$\endgroup$
$\begingroup$
$\endgroup$
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
$\begingroup$
$\endgroup$
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]
To crop the bottom of the bars:
modifyF@bc
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]] &;
-
$\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
ListLinePlot
withFilling
option? $\endgroup$ – BlacKow Apr 19 '16 at 16:22