2
$\begingroup$

I want to plot an overlapped graphic comparing the experimental and theoretical data of the number of counts in a given time (beta decay), so I show them in a histogram.

Because of sometimes the number of counts is 0, the experimental histogram has a bin centered at 0.

v = ListPlot[{{Around[M, S], 34/2}}, PlotStyle -> {Dashed, Thick, Black}];
l = Graphics[{Dashed, Thick, {Line[{{M, 0}, {M, 34}}], Line[{{M - S, 34/2}, {M + S, 34/2}}]}}];
h = Histogram[t, Frame -> True,  FrameLabel -> {"Cuentas / 20 s", "Frecuencia"},  FrameStyle ->    Directive[GrayLevel[0], 20, FontFamily -> "LM Roman 12",     AbsoluteThickness[1.0`]],  ImageSize -> 600,  FrameTicks -> {{Table[i, {i, 0, 35, 5}],      None}, {Table[i, {i, 0, 8}], None}},[![enter image description here][1]][1]  PlotRange -> {{-0.5, 8.5}, Automatic}]
m1 = Show[h, l, v]

The problem appears when I build the theortical plot. I have a list of calculated heights of the bins (included 0 counts), so I use a BarChart

B = BarChart[Fteor, BarSpacing -> None, Frame -> True,   FrameLabel -> {"Cuentas / 20 s", "Frecuencia"},   FrameStyle -> 
Directive[GrayLevel[0], 20, FontFamily -> "LM Roman 12", 
 AbsoluteThickness[1.0`]],   ImageSize -> 600];
A = ListPlot[{{Around[M, J], Max[Fteor]/2}}, PlotStyle -> {Dashed, Thick, Black}];
G = Graphics[{Dashed, Black, Thick, Line[{{M, 0}, {M, Max[Fteor]}}]}];[![enter image description here][1]][1]

m2 = Show[B, A, G]

And if I combine them:

Show[m1,m2]

As you can see, the theoretical BarChart put the first bin (0 events) at position 1. I also have added other graphics to view the experimental and theoretical standard deviation and mean of distribution (Variables S, J, M, respectively) so I don't want to edit the rest of elements, only BarChart (for simplicity).

Is there a simple way to tell Mathematica to put the first bin of a BarChart centered at 0 instead of 1?

P.S.: I have already seen solutions of creating Rectangles with given height and position, but I am looking for a simple solution with BarChart (if it is posible)

Thanks! Dani

$\endgroup$
  • 1
    $\begingroup$ An alternative would be to use Histogram instead of BarChart - this way, you get the same look for free. You can supply the data to Histogram as WeightedData[xlist,ylist], where xlist is the bin position, and ylist is the bin height $\endgroup$ – Lukas Lang Oct 16 at 15:54
2
$\begingroup$

You can post-process the BarChart output to change the coordinates of Rectangles:

barheights = {2, 1, 3};

bc = Show[BarChart[barheights , BarSpacing -> 0, Axes -> False], 
  ListLinePlot[{{1, 3}, {2, 1}, {3, 2}}, PlotStyle -> Red], 
  Frame -> True]

enter image description here

bc /. Rectangle[a_, b_, c___] :>  Rectangle[{-1, 0} + a, {-1, 0} + b, c]

enter image description here

An alternative approach using Histogram with barheights& as the height specification:

binning = {0.5, 3.5, 1};
Show[Histogram[{1}, binning, barheights &], 
 ListLinePlot[{{1, 3}, {2, 1}, {3, 2}}, PlotStyle -> Red], 
   Frame -> True, PlotRange -> All]

enter image description here

You can modify the binning specification to move the rectangles:

Show[Histogram[{1}, binning - {1, 1, 0}, barheights &], 
 ListLinePlot[{{1, 3}, {2, 1}, {3, 2}}, PlotStyle -> Red], 
   Frame -> True, PlotRange -> All]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks! I personally used the aproach with Histogram BarHeight &. Very simple solution! $\endgroup$ – Dani Doménech Oct 16 at 19:53
  • $\begingroup$ @DaniDoménech, my pleasure. Thank you for the accept. $\endgroup$ – kglr Oct 16 at 19:54
2
$\begingroup$

An alternative approach using Histogram and WeightedData, as described in the comments:

barheights = {2, 1, 3};

Show[
 Histogram[
  WeightedData[Range@Length@barheights - 1, barheights],
  Length@barheights
 ],
 ListLinePlot[{{1, 3}, {2, 1}, {3, 2}}, PlotStyle -> Red],
 Frame -> True,
 PlotRange -> All
 ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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