3
$\begingroup$

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`"]

dadosGtop = Import["Gtop_PR.dat", "table"];

costhbGtop = Table[dadosGtop[[i]][[1]], {i, 1, Length[dadosGtop]}];

costhleGtop = Table[dadosGtop[[i]][[2]], {i, 1, Length[dadosGtop]}];


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, 
    LegendShadow -> None, 
    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:

dadosGtop={
   {-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}
   }
$\endgroup$
8
  • $\begingroup$ Hello, can You show us data You are working with ot at least form of the data? $\endgroup$
    – Kuba
    Commented Jul 15, 2013 at 12:41
  • $\begingroup$ Sorry, I was going to post the code but there is characters limit! I'm using simple raw data from some file. $\endgroup$
    – Miguel
    Commented Jul 15, 2013 at 12:42
  • $\begingroup$ just added the code in the question $\endgroup$
    – Miguel
    Commented Jul 15, 2013 at 12:51
  • $\begingroup$ Could You provide a data sample? $\endgroup$
    – Kuba
    Commented Jul 15, 2013 at 12:55
  • $\begingroup$ added in the question $\endgroup$
    – Miguel
    Commented Jul 15, 2013 at 13:03

2 Answers 2

3
$\begingroup$

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, 
                             ImagePadding -> 15, PlotRangePadding -> [email protected]]
            },
            Column[
                   ListLinePlot[##, opts] & @@@ {
                                   {dat[[;; 2]], PlotStyle -> {{Green}, {Dashed, Blue}}},
                                   {Last@dat}}

                  ]
           ]
       ]

enter image description here

$\endgroup$
0
$\begingroup$

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}]

$\endgroup$
1
  • $\begingroup$ Ok, many thanks. I'll try it. $\endgroup$
    – Miguel
    Commented Jul 16, 2013 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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