Here's some template code for plotting histograms that don't look like bar charts, but instead have only outlines (like IDL creates).

First define a function that does the bulk of the heavy lifting.

   1 import numpy as np
   2 
   3 def histOutline(dataIn, *args, **kwargs):
   4     (histIn, binsIn) = np.histogram(dataIn, *args, **kwargs)
   5 
   6     stepSize = binsIn[1] - binsIn[0]
   7 
   8     bins = np.zeros(len(binsIn)*2 + 2, dtype=np.float)
   9     data = np.zeros(len(binsIn)*2 + 2, dtype=np.float)
  10     for bb in range(len(binsIn)):
  11         bins[2*bb + 1] = binsIn[bb]
  12         bins[2*bb + 2] = binsIn[bb] + stepSize
  13         if bb < len(histIn):
  14             data[2*bb + 1] = histIn[bb]
  15             data[2*bb + 2] = histIn[bb]
  16 
  17     bins[0] = bins[1]
  18     bins[-1] = bins[-2]
  19     data[0] = 0
  20     data[-1] = 0
  21 
  22     return (bins, data)

Now we can make plots:

   1 
   2 # Make some data to plot
   3 data = randn(500)
   4 
   5 figure(2, figsize=(10, 5))
   6 clf()
   7 
   8 ##########
   9 #
  10 # First make a normal histogram
  11 #
  12 ##########
  13 subplot(1, 2, 1)
  14 (n, bins, patches) = hist(data)
  15 
  16 # Boundaries
  17 xlo = -max(abs(bins))
  18 xhi = max(abs(bins))
  19 ylo = 0
  20 yhi = max(n) * 1.1
  21 
  22 axis([xlo, xhi, ylo, yhi])
  23 
  24 ##########
  25 #
  26 # Now make a histogram in outline format
  27 #
  28 ##########
  29 (bins, n) = histOutline(data)
  30 
  31 subplot(1, 2, 2)
  32 plot(bins, n, 'k-')
  33 axis([xlo, xhi, ylo, yhi])

Here you can find this functionality packaged up into histOutline.py

Cookbook/Matplotlib/UnfilledHistograms (last edited 2009-07-01 22:47:50 by MarshallPerrin)