Tooltipping Histograms: what's in the bin?

Cool question. I can find a way to get Histogram to produce the correct tooltip internally, so I've resorted to a little formatting outside, then pointing LabelingFunction to the correctly formatted labels.

First I need to get the list of data points in each histogram bin:

{bins, vals} = HistogramList[a];
{aList, bList} = BinLists[#, {bins}] & /@ {a, b}

{{{4, 0, 4, 0, 2}, {7, 5, 9, 9}, {10}}, {{3, 0, 1, 2}, {5, 6, 5, 7, 7}, {10}}}

Then I use these lists to split the data point labels into the same list structure (there may be a more elegant / efficient way to do this):

labelLists = Function[{ds}, 
    Flatten[Pick[ds[[2]], ds[[1]], #] & /@ Union[#]] & /@ ds[[3]]
  ] /@ Transpose[{{a, b}, {aLabels, bLabels}, {aList, bList}}]

{{{"a: a", "a: x", "a: e", "a: f", "a: v"}, {"a: e", "a: b", "a: b", "a: s"}, {"a: h"}}, {{"b: h", "b: t", "b: s", "b: f"}, {"b: c", "b: o", "b: s", "b: c", "b: v"}, {"b: e"}}}

In this format we can use the second argument (#2) to LabelingFunction to access the correct labels to show for a given histogram bar:

Histogram[{a, b}, {bins}, 
 LabelingFunction -> 
  (Placed[TableForm@{labelLists[[1 ;; #2[[1]], #2[[2]]]]}, Tooltip] &)
]

The extra jiggery-pokery in there (the 1;;#2[[1]] etc) is so that overlapping bars display correctly:

enter image description here enter image description here


The example data you gave has a highly overlapped area. Your real example might not have the same signature, but I would think that PairedHistogram might be a better visualization for the case. Anyway, no matter it is PairedHistogram or Histogram, you can use the customizability of ChartElementFunction to create your own histogram bars. The arguments that get fed in the ChartElementFuncion contain the information of data in each bin.

Here is an example:

f[{x_, y_}, data_, r__] := Tooltip[Rectangle @@ Transpose[{x, y}], data]
a = RandomInteger[10, 10];
b = RandomInteger[10, 10];
PairedHistogram[a, b, ChartElementFunction -> f]

then you can format the list however you want. There are a lot of ways to format lists in WL. Look up TableForm and there are related functions in the bottom of the doc.

enter image description here