home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Mathematics / Notebooks / IP_Histogram / histogram.m < prev    next >
Text File  |  1992-08-17  |  4KB  |  119 lines

  1. BeginPackage["Histogram`"]
  2.  
  3. Histogram::usage = "Histogram[Data_?VectorQ,Bins_MatrixQ,BinLabels_List:{},ShowLabels_:True] produces a histogram of Data, using Bins to group the data. Bins is a list of data pairs, listing the minimum and maximum values for each bin. BinLabels is an optional list of strings or expressions to use as labels below the bars. If the bar widths are too small to have labels, set the optional parameter ShowLabels to False.  The bin widths need not be equal. The area of the bar corresponds to the percentage of data in the bin (the height of the bar may not show the percentage). Very little validity checking is done on the bins.
  4.  
  5. Examples: Histogram[{2,3,4,2,3,1,5,3,7},{{0,2},{4,6},{2,4},{6,8}},{Aardvarks,Bats,\"Cows\",6+3}]
  6.  
  7. Histogram[Table[Random[],{i,50}],Table[{i,i+.05},{i,0,.95,.05}],False]
  8.       
  9.       Histogram[Table[Random[],{i,1000}],{{0,.2},{.2,.5},{.5,.6},{.6,.95},{.95,1}},{a,b,c,d,e}]"
  10.  
  11. (* This package may be freely used for non-commercial purposes.
  12.    
  13.                    Copyright August 1992
  14.            
  15.                    Dr. Eric Gossett
  16.                    Bethel College
  17.                    3900 Bethel Drive
  18.                    St. Paul, MN 55112
  19.                    gossett@bethel.edu
  20.            
  21.    Version 1.1
  22. *)
  23.  
  24. Begin["`Private`"]
  25. Unprotect[Histogram]
  26. Clear[Histogram];
  27.  
  28. Histogram[Data_?VectorQ,Bins_,BinLabels_List:{},ShowLabels_:True] := 
  29. Module[{BinCenters,Total,i,NumBins,Percents,SBins,Heights,
  30.         MaxHeight, graph},
  31.   
  32.   (* turn off spell checking *)
  33.  
  34.   Off[General::spell1];
  35.   
  36.   (* Compute secondary values *)
  37.   
  38.   SBins = Sort[Bins];  (* This will slow things down if there are many bins *)
  39.   Total = Length[Data];
  40.   NumBins = Length[SBins];
  41.   BinCenters = Map[(N[#[[1]]+(#[[2]]-#[[1]])/2])&,SBins];
  42.   Minx = SBins[[1,1]];
  43.   Maxx = SBins[[NumBins,2]];
  44.   
  45.   (* Count the number and % of data values in each bin *)
  46.   
  47.   BinCounts = Table[0,{i,NumBins}];
  48.   For[i=1,i<=NumBins,i++,
  49.     BinCounts[[i]] = Length[Select[Data,
  50.       ((#>SBins[[i,1]])&&(#<=SBins[[i,2]]))&]];
  51.   ];
  52.   
  53.   Percents = N[BinCounts/Total];
  54.   
  55.   (* Compute the bar heights *)
  56.   
  57.   Heights = Table[
  58.            N[Percents[[i]]/(SBins[[i,2]] - SBins[[i,1]])],
  59.                {i,NumBins}];
  60.   MaxHeight = Max[Heights];
  61.   
  62.   (* Produce default lables *)
  63.  
  64.   If[BinLabels == {},Labels = SBins,Labels=BinLabels];
  65.   
  66.   (* Plot the histogram *)
  67.   
  68.  graph = Show[
  69.     (* The filled bars *)
  70.     
  71.     Graphics[Prepend[Table[
  72.         Polygon[{{SBins[[i,1]],0},{SBins[[i,1]],Heights[[i]]},
  73.               {SBins[[i,2]],Heights[[i]]},{SBins[[i,2]],0}}],
  74.           {i,NumBins}],GrayLevel[.6667]]],
  75.     
  76.     (* The lines around the edges *)
  77.     
  78.     Graphics[Prepend[Table[
  79.         Line[{{SBins[[i,1]],0},{SBins[[i,1]],Heights[[i]]},
  80.               {SBins[[i,2]],Heights[[i]]},{SBins[[i,2]],0}}],
  81.           {i,NumBins}],Thickness[.002]]],
  82.     
  83.     (* the x-axis *)
  84.     
  85.     Graphics[{Thickness[.002],Line[{{Minx,0},{Maxx,0}}]}],
  86.     
  87.     (* the x-axis (bin) labels *)
  88.     
  89.     If[ShowLabels,Graphics[Table[
  90.           Text[Labels[[i]],{BinCenters[[i]],-.02 MaxHeight},
  91.               {0,1}],
  92.        {i,NumBins}]],Graphics[]],
  93.        
  94.     (* the bar labels (percents ) *)
  95.     
  96.     If[ShowLabels,Graphics[Table[
  97.       Text[ToString[NumberForm[100Percents[[i]],{5,60}]] <> "%",
  98.         {BinCenters[[i]], Heights[[i]] + .02 MaxHeight},{0,-1}],
  99.        {i,NumBins}]],Graphics[]],
  100.  
  101.     (* Kludge to get labels to print without being clipped *)
  102.         
  103.     Graphics[{GrayLevel[1],Line[{{SBins[[1,1]],-.01 MaxHeight},
  104.        {SBins[[1,1]],-.05 MaxHeight}}],GrayLevel[0]}],
  105.     Graphics[{GrayLevel[1],
  106.         Line[{{SBins[[1,1]],MaxHeight+.01 MaxHeight},
  107.        {SBins[[1,1]],MaxHeight+.05 MaxHeight}}],GrayLevel[0]}]
  108.   ];
  109.   
  110.   (* turn spell checking on again *)
  111.   
  112.   On[General::spell1];
  113.  
  114.   Return[graph]
  115. ]
  116.  
  117. End[]
  118. Protect[Histogram]
  119. EndPackage[]