 # plotting overlapped histograms

Hi folks,

I'm interested in hearing some strategies for plotting two histograms on the same plot. My goal is
to visualize and compare two distributions which have similar shapes.

My first attempt was to use alpha, but this isn't well support on PS2 output driver (surprising)
and I will be including these in a paper, so having a good print output format like PS2 or PDF is desired.
PDF seems to work ok, but the alpha model doesn't completely satisfy my in areas where the two
plots overlap a good deal.

import pylab, random

d1 = [random.gauss(0, 1) for i in range(100)]
d2 = [random.gauss(1, 1) for i in range(100)]

color = ['r', 'g']
pylab.hist(d1, facecolor='r', edgecolor='black', alpha=0.75)
pylab.hist(d2, facecolor='b', edgecolor='black', alpha=0.75)
pylab.savefig('test.pdf')
pylab.show()

What ideas do other people have? My thought was to subdivide the plot into two subplots, and
plot one distribution on top, and the other on the bottom, but this effectively halves my vertical resolution
(I'm already plotting 24 subplots per page).

Thanks,
Dave

To compare two histograms you can plot a bihistogram as suggested on
http://www.itl.nist.gov/div898/handbook/eda/section3/bihistog.htm

The little function I've written to do so is below. See if it helps.

Antonio

import scipy
from pylab import figure

def bihist(y1, y2, nbins=10, h=None):
'''
Bihistogram.
h is an axis handle. If not present, a new figure is created.
'''
if h is None: h = figure().add_subplot(111)
xmin = scipy.floor(scipy.minimum(y1.min(), y2.min()))
xmax = scipy.ceil(scipy.maximum(y1.max(), y2.max()))
bins = scipy.linspace(xmin, xmax, nbins)
n1, bins1, patch1 = h.hist(y1, bins)
n2, bins2, patch2 = h.hist(y2, bins)
# set ymax:
ymax = 0
for i in patch1:
height = i.get_height()
if height > ymax: ymax = height
# invert second histogram and set ymin:
ymin = 0
for i in patch2:
height = i.get_height()
height = -height
i.set_height(height)
if height < ymin: ymin = height
h.set_ylim(ymin*1.1, ymax*1.1)
h.figure.canvas.draw()