Cookbook/Matplotlib/UnfilledHistograms - SciPy wiki dump

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

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

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

hist_outline.png

SciPy: Cookbook/Matplotlib/UnfilledHistograms (last edited 2015年10月24日 17:48:23 by anonymous)

AltStyle によって変換されたページ (->オリジナル) /