This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Attachment 'histOutline.py'

Download

   1 # code from http://www.scipy.org/Cookbook/Matplotlib/UnfilledHistograms?action=AttachFile&do=get&target=histNofill.py
   2 from matplotlib import pylab
   3 import numpy as np
   4 
   5 
   6 
   7 def histOutline(dataIn, *args, **kwargs):
   8     """
   9     Make a histogram that can be plotted with plot() so that
  10     the histogram just has the outline rather than bars as it
  11     usually does.
  12 
  13     Example Usage:
  14     binsIn = numpy.arange(0, 1, 0.1)
  15     angle = pylab.rand(50)
  16 
  17     (bins, data) = histOutline(binsIn, angle)
  18     plot(bins, data, 'k-', linewidth=2)
  19 
  20     """
  21 
  22     (histIn, binsIn) = np.histogram(dataIn, *args, **kwargs)
  23 
  24     stepSize = binsIn[1] - binsIn[0]
  25 
  26     bins = np.zeros(len(binsIn)*2 + 2, dtype=np.float)
  27     data = np.zeros(len(binsIn)*2 + 2, dtype=np.float)    
  28     for bb in range(len(binsIn)):
  29         bins[2*bb + 1] = binsIn[bb]
  30         bins[2*bb + 2] = binsIn[bb] + stepSize
  31         if bb < len(histIn):
  32             data[2*bb + 1] = histIn[bb]
  33             data[2*bb + 2] = histIn[bb]
  34 
  35     bins[0] = bins[1]
  36     bins[-1] = bins[-2]
  37     data[0] = 0
  38     data[-1] = 0
  39     
  40     return (bins, data)
  41 
  42 
  43 
  44 if __name__ == "__main__":
  45     binsIn = np.arange(0, 1, 0.1)
  46     angle = pylab.rand(50)
  47 
  48     pylab.subplot(121)
  49     pylab.hist(angle,binsIn)
  50     pylab.title("regular histogram")
  51     pylab.axis(xmax=1.0)
  52 
  53     pylab.subplot(122)
  54 
  55     (bins, data) = histOutline(angle, binsIn)
  56     pylab.plot(bins, data, 'k-', linewidth=2)
  57     pylab.title("histOutline Demo")
  58     pylab.show()

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.