2
$\begingroup$

I plot three histograms (originating from a large Dataset ds) in one graph

  Histogram[{#"Var1" & /@ Normal[ds[Select[#"Var2" == 1 &]]],
  #"Var1" & /@ Normal[ds[Select[#"Var2" == 2 &]]],
  #"Var1" & /@ Normal[ds[Select[#"Var2" == Null &]]]},
  {0, 50, 2} , "Frame" -> True, 
  "FrameLabel" -> {"performance", "counts"}, 
  "ChartLegends" -> {"XY: test 1", "XY: test 2", "XY: unknown" },
  "ChartStyle" -> {Red, Green, Blue}, "PlotLabel" -> "Test"]

and want to display the number of entries per histogram in the legend (where the XY can be seen).enter image description here

But I didn't find a way to get the number of entries per histogram. Is there a function existing?

$\endgroup$

2 Answers 2

2
$\begingroup$

You can use BinCounts for that purpose, e.g. in that fashion:

Block[{bw = {0, 50, 2},
  ds1 = #"Var1" & /@ Normal[ds[Select[#"Var2" == 1 &]]], 
  ds2 = #"Var1" & /@ Normal[ds[Select[#"Var2" == 2 &]]], 
  ds3 = #"Var1" & /@ Normal[ds[Select[#"Var2" == Null &]]],
  bc1, bc2, bc3},
 {bc1, bc2, bc3} = (ToString@Total@BinCounts[#, bw]) & /@ {ds1, ds2, ds3};
 Histogram[{ds1, ds2, ds3}, bw, "Frame" -> True, 
  "FrameLabel" -> {"performance", "counts"}, 
  "ChartLegends" -> {bc1<>": test 1", bc2<>": test 2", bc3<>": unknown"},
  "ChartStyle" -> {Red, Green, Blue}, 
  "PlotLabel" -> "Test"]
 ]
$\endgroup$
2
  • $\begingroup$ Perfect, thank you. Meanwhile I found BinCounts but not a proper way to use it. $\endgroup$
    – Lea
    Commented Feb 20, 2015 at 11:06
  • $\begingroup$ You are most welcome! $\endgroup$
    – Jinxed
    Commented Feb 20, 2015 at 11:11
1
$\begingroup$

I'm not exactly sure what you want but here's the solution to a possible interpretation.

Here are some test data

ds = RandomVariate[NormalDistribution[1, 1], {10^4}];

The histogram of which is

h = Histogram[ds];
(* output not shown here *)

Now there's a function HistogramList[] which gives

hlst = HistogramList[ds]

(* Out[324]= {{-(13/5), -(12/5), -(11/5), -2, -(9/5), -(8/5), -(7/5), -(6/5), -1, -(4/
   5), -(3/5), -(2/5), -(1/5), 0, 1/5, 2/5, 3/5, 4/5, 1, 6/5, 7/5, 8/5, 9/5, 
  2, 11/5, 12/5, 13/5, 14/5, 3, 16/5, 17/5, 18/5, 19/5, 4, 21/5, 22/5, 23/5, 
  24/5, 5}, {3, 3, 11, 17, 21, 42, 59, 81, 121, 195, 264, 338, 426, 558, 639, 
  701, 736, 810, 787, 694, 719, 658, 567, 419, 325, 252, 184, 126, 90, 64, 45,
   21, 12, 4, 3, 0, 2, 3}} *)

from which the number of entries in the histogram is easyly extracted:

Length[hlst[[2]]]

(* Out[325]= 38 *)
$\endgroup$

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.