# two histograms ratio

I have two histograms and using listplot to draw both. Now, I want to draw a second plot, below the first, with the ratio of each bin. Does anyone know how to do this?

Many thanks.

This is the code I'm using to draw the histograms:

Needs["PlotLegends`"]

histPlot[data_, bins_, origin_, color_, label_, title_, nome_] :=
Module[{countBorder1 =
Partition[Riffle[Riffle[#1, #1[[2 ;;]]], Riffle[#2, #2]], 2] & @@
HistogramList[data[[1]], bins],
countBorder2 =
Partition[Riffle[Riffle[#1, #1[[2 ;;]]], Riffle[#2, #2]], 2] & @@
HistogramList[data[[2]], bins],
},
ListLinePlot[{countBorder1, countBorder2}, PlotRange -> All,
PlotStyle -> {{color[[1]]}, {color[[2]], Dashed}},
AxesLabel -> {Style[label[[1]], 14, Bold],
Style[label[[2]], 14, Bold]}, AxesOrigin -> origin,
LegendPosition -> {0.65, 0.1}, LegendSize -> 1,
PlotLegend -> {Style[nome[[1]], 12, Bold],
Style[nome[[2]], 12, Bold]},
PlotLabel -> Style[title, Black, 14, Bold],
TicksStyle -> {16, Bold}, ImageSize -> Scaled[0.6]]];

histPlot[{costhbGtop, costhneGtop}, {-1, 1, 0.1}, {-1, 0}, {Green, Blue}, {"\[Eta]",
"\!$$\*FractionBox[\(dN$$, $$d\[Eta]$$]\)"}, "Pseudorapidity of b", \
{"b", "ne"}]

Here is a data sample:

{-0.68, 0.536, 0.316}, {-0.394, 0.187, 0.287}, {-0.874, 0.753, 0.474},
{-0.202, 0.18, 0.066}, {-0.267,  0.894, -0.077}, {0.126, -0.144, -0.063},
{0.952,  0.63, -0.953}, {0.671, 0.073, -0.84}, {-0.344,  0.72, -0.024},
{0.824, -0.205, -0.741}, {0.718, -0.793, -0.268}, {-0.423, 0.568, -0.204},
{-0.192, -0.096, 0.357}, {-0.03,   0.865, -0.629}, {-0.784, 0.39, 0.588},
{0.98, 0.59, -0.953},   {-0.97,  0.682, 0.653}, {-0.654, 0.907, -0.201},
{-0.35,   0.712, -0.176}, {-0.929, 0.654, 0.48}, {-0.479, 0.826, 0.099},
{-0.54, 0.785,-0.724}, {-0.812, 0.818,-0.069}, {-0.988, -0.083, 0.89},
{-0.559, 0.976,-0.188}, {0.748, 0.211,-0.994}, {0.507, -0.527,-0.266},
{0.776, -0.025,-0.964}
}
• Hello, can You show us data You are working with ot at least form of the data?
– Kuba
Commented Jul 15, 2013 at 12:41
• Sorry, I was going to post the code but there is characters limit! I'm using simple raw data from some file. Commented Jul 15, 2013 at 12:42
• just added the code in the question Commented Jul 15, 2013 at 12:51
• Could You provide a data sample?
– Kuba
Commented Jul 15, 2013 at 12:55
• added in the question Commented Jul 15, 2013 at 13:03

What would be the ratio for n/0 or 0/0. You can set easily in definition below:

SetAttributes[f, Listable]
f[0, 0] := 0;
f[_, 0] := 0;
f[x_, y_] := x/y;

If you want to stick with your Module, use Column as george2079 has said. Divisions are simply:

f[counts1, counts2]

I find following version a little more compact but the choice is yours.

data = {{-0.68, 0.536, 0.316}, {-0.394, 0.187, 0.287}, {-0.874, 0.753, 0.474},
{-0.202, 0.18, 0.066}, {-0.267, 0.894, -0.077}, {0.126, -0.144, -0.063},
{0.952, 0.63, -0.953},  {0.671, 0.073, -0.84}, {-0.344, 0.72, -0.024},
{0.824, -0.205, -0.741}, {0.718, -0.793, -0.268}, {-0.423, 0.568, -0.204},
{-0.192, -0.096, 0.357}, {-0.03, 0.865, -0.629}, {-0.784, 0.39, 0.588},
{0.98, 0.59, -0.953}, {-0.97, 0.682, 0.653}, {-0.654,0.907, -0.201},
{-0.35, 0.712, -0.176}, {-0.929, 0.654, 0.48}, {-0.479, 0.826, 0.099},
{-0.54, 0.785, -0.724}, {-0.812, 0.818, -0.069}, {-0.988, -0.083, 0.89},
{-0.559, 0.976, -0.188}, {0.748, 0.211, -0.994}, {0.507, -0.527, -0.266},
{0.776, -0.025, -0.964}};

bg = data[[;; , 1]];
nl = data[[;; , 2]];

Module[{d, r, dat},
d = BinCounts[#, {-1.1, 1.1, .1}] & /@ {bg, nl};
r = Range[-1, 1, .1] /. n_Real :> {n, n};
dat = Flatten[Transpose /@ Transpose[{r, Partition[#, 2, 1]}], 1
] & /@ (d~Join~{f @@ d});

With[{opts = Sequence[Axes -> False, Frame -> True, ImageSize -> 300,
},
Column[
ListLinePlot[##, opts] & @@@ {
{dat[[;; 2]], PlotStyle -> {{Green}, {Dashed, Blue}}},
{Last@dat}}

]
]
]

the ratio plot:

ListLinePlot[
MapThread[{#1[[1]], If[#2[[2]] != 0 ,  #1[[2]]/#2[[2]], 0 ]} &,
{countBorder1 ,   countBorder2 }] ]

A simple way to put the plots together is to use GraphicsColumn[{plot1,plot2}]

• Ok, many thanks. I'll try it. Commented Jul 16, 2013 at 13:02