11
$\begingroup$

Is there a way of making a bihistogram plot in mathematica?

There is a reference to it at http://mathworld.wolfram.com/Bihistogram.html but no suggestions as to how to do it.

$\endgroup$
15
$\begingroup$
data1 = RandomVariate[NormalDistribution[0, 1], 1000];
data2 = RandomVariate[NormalDistribution[0.3, 1], 1000];

One way is PairedHistogram

PairedHistogram[data1, data2]

enter image description here

A different layout can be achieved by hacking the height specification of Histogram.

Show[
 Histogram[data1, Automatic],
 Histogram[data2, Automatic, -#2 &]
 ]

enter image description here


Update

In practice, we often deal with datasets of different sizes. Thus instead of the "Count" height specification, we may want "PDF" or something else.

Here I provide explicit functions for some of the usual height specifications.

count = #2;
pdf = -(#2/(Total[#2] Subtract @@@ #1)) &;
probability = #2/Total[#2] &;
intensity = -(#2/Subtract @@@ #1) &;

These correspond exactly to the specifications "Count", "PDF", "Probability" and "Intensity", but they are expressed as a function, so we can easily put a minus sign in front to flip the bars.

Example with datasets of different sizes:

data1 = RandomVariate[NormalDistribution[0, 1.5], 1000];
data2 = RandomVariate[NormalDistribution[1, 0.7], 10000];

bs = 0.2; (* manually fixed bin size *)
Show[
 Histogram[data1, {bs}, pdf (* or "PDF" *)],
 Histogram[data2, {bs}, -pdf[##] &],
 PlotRange -> 0.7 {-1, 1} (*manual plot range to center axis *)
 ]

enter image description here

$\endgroup$
  • $\begingroup$ I really like the Histogram hight specification solution! $\endgroup$ – Anton Antonov May 2 '16 at 14:20
  • 2
    $\begingroup$ And a simple trick to get different colours in the second example (while using Mathematica's default palette) would be Show[Histogram[data1, Automatic], Histogram[{{}, data2}, Automatic, -#2 &]] $\endgroup$ – Martin Ender May 2 '16 at 15:22
  • $\begingroup$ I think this will work very nicely. Thanks very much. One small additional question for the rotated plot -- how can I ensure that the bin width, height and range are the same for both histograms? $\endgroup$ – Peter Blunden May 3 '16 at 4:56
  • $\begingroup$ The data I am plotting has one histogram that is higher and has a longer tail than the other one. I'd actually like the option of either centering the horizontal axis or adjusting it up or down. $\endgroup$ – Peter Blunden May 3 '16 at 5:03
  • $\begingroup$ @PeterBlunden Use a manual bin(-size) specification for both to ensure the same bin size (second argument). You can use PlotRange in Show to set the vertical range (and thus set the visual location of the center axis which is at 0). $\endgroup$ – Szabolcs May 3 '16 at 7:35

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.