I've made some alterations to the hist() function in axes.py (attached
is a diff against the current SVN version).  I've added the capability
to use the same interface to make outline histograms instead of bar
histograms (and a third option for outlines with fill - note that this
is NOT the same as calling it twice with the other two, as the widths
are slightly different).  There's a slight compatibility issue in that
as it stands in that the returned tuple now has 4 values (I added a
list of the lines that are generated if the steps command is used),
but I can't really imagine how that could break anything but the
poorest-written code...  Anyone think this is worth committing to SVN?

(One thing that bothers me a little is the part of the code that adds
the last two edges to the histogram - the problem is that if you have
a line size greater than 1, the outline overshoots the rest of the
outline by a very tiny bit... if anyone knows how to cut off the upper
row of pixels to make it flush with the rest of the outline... it's
perfectly usable as-is, though - that's just a tiny little aesthetic
quibble)

-- 
Erik Tollerud
Graduate Student
Center For Cosmology
Department of Physics and Astronomy
4155B Frederick Reines Hall
University of California, Irvine
Office Phone: (949)824-2996
Cell: (651)307-9409
[EMAIL PROTECTED]
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py	(revision 5027)
+++ lib/matplotlib/axes.py	(working copy)
@@ -5168,15 +5168,14 @@
 
 
     #### Data analysis
-
-
-    def hist(self, x, bins=10, normed=0, bottom=None,
+    
+    def hist(self, x, bins=10, normed=0, bottom=None, histtype='bar',
              align='edge', orientation='vertical', width=None,
-             log=False, **kwargs):
+             log=False, lineprops={},**kwargs):
         """
-        HIST(x, bins=10, normed=0, bottom=None,
+        HIST(x, bins=10, normed=0, bottom=None, histtype='bar',
              align='edge', orientation='vertical', width=None,
-             log=False, **kwargs)
+             log=False, lineprops={},**kwargs)
 
         Compute the histogram of x.  bins is either an integer number of
         bins or a sequence giving the bins.  x are the data to be binned.
@@ -5192,6 +5191,10 @@
           # trapezoidal integration of the probability density function
           pdf, bins, patches = ax.hist(...)
           print npy.trapz(pdf, bins)
+          
+        histtype = 'bar' | 'step' | 'stepfill'.  The type of histogram
+        to draw - 'bar' is a traditional bar-type histogram, 'step' 
+        generates a lineplot 
 
         align = 'edge' | 'center'.  Interprets bins either as edge
         or center values
@@ -5200,28 +5203,103 @@
         will be used and the "bottom" kwarg will be the left edges.
 
         width: the width of the bars.  If None, automatically compute
-        the width.
+        the width. Ignored for 'step' type.
 
         log: if True, the histogram axis will be set to a log scale
+        
+        lineprops: a mapping to update the properties of the line
+        for the 'step' or 'stepfill' type.
+        %(Line2D)s
 
         kwargs are used to update the properties of the
-        hist Rectangles:
+        hist Rectangles for 'bar' or 'stepfill' type.
         %(Rectangle)s
         """
         if not self._hold: self.cla()
         n, bins = npy.histogram(x, bins, range=None, normed=normed)
-        if width is None: width = 0.9*(bins[1]-bins[0])
-        if orientation == 'horizontal':
-            patches = self.barh(bins, n, height=width, left=bottom,
-                                align=align, log=log)
-        elif orientation == 'vertical':
-            patches = self.bar(bins, n, width=width, bottom=bottom,
-                                align=align, log=log)
+        if width is None: 
+            if 'step' in histtype:
+                width = (bins[1]-bins[0])
+                kwargs['lw']=0 #make the fill lines non-existent 
+            else:
+                width = 0.9*(bins[1]-bins[0])
+        if 'step' in histtype:
+            if orientation == 'horizontal':
+                if align=='edge':
+                    lines=self.plot(npy.concatenate((n[-1:],n)),
+                                    npy.concatenate((bins,bins[-1:]+width)),
+                                    ls='steps-post')
+                    #add lines for the right and left edges
+                    lines.append(mlines.Line2D([0,n[0]],[bins[0],bins[0]]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                    lines.append(mlines.Line2D([0,n[-1]],[bins[-1:]+width,bins[-1:]+width]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                else:
+                    lines=self.plot(npy.concatenate((n[-1:],n)),
+                                    npy.concatenate((bins,bins[-1:]+width))-width/2,
+                                    ls='steps-post')
+                    #add lines for the right and left edges
+                    lines.append(mlines.Line2D([0,n[0]],[bins[0]-width/2,bins[0]-width/2]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                    lines.append(mlines.Line2D([0,n[-1]],[bins[-1:]+width/2,bins[-1:]+width/2]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                
+            elif orientation == 'vertical':
+                if align=='edge':
+                    lines=self.plot(npy.concatenate((bins,bins[-1:]+width)),
+                                npy.concatenate((n,n[-1:])),ls='steps-post')
+                    #add lines for the right and left edges
+                    lines.append(mlines.Line2D([bins[0],bins[0]],[0,n[0]]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                    lines.append(mlines.Line2D([bins[-1:]+width,bins[-1:]+width],[0,n[-1]]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                else:
+                    lines=self.plot(npy.concatenate((bins,bins[-1:]+width))-width/2,
+                                npy.concatenate((n,n[-1:])),ls='steps-post')
+                    #add lines for the right and left edges
+                    lines.append(mlines.Line2D([bins[0]-width/2,bins[0]-width/2],[0,n[0]]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+                    lines.append(mlines.Line2D([bins[-1:]+width/2,bins[-1:]+width/2],[0,n[-1]]))
+                    lines[-1].update_from(lines[0])
+                    lines[-1].set_ls('-')
+                    self.add_line(lines[-1])
+            else:
+                raise ValueError, 'invalid orientation: %s' % orientation
+            for l in lines:
+                l.update(lineprops)
         else:
-            raise ValueError, 'invalid orientation: %s' % orientation
-        for p in patches:
-            p.update(kwargs)
-        return n, bins, cbook.silent_list('Patch', patches)
+            lines=[]
+            
+        if histtype != 'step':
+            if orientation == 'horizontal':
+                patches = self.barh(bins, n, height=width, left=bottom,
+                                    align=align, log=log)
+            elif orientation == 'vertical':
+                patches = self.bar(bins, n, width=width, bottom=bottom,
+                                    align=align, log=log)
+            else:
+                raise ValueError, 'invalid orientation: %s' % orientation
+            for p in patches:
+                p.update(kwargs)
+        else:
+            patches=[]
+            
+        return n, bins, cbook.silent_list('Patch', patches),cbook.silent_list('Line2D', lines)
+        
     hist.__doc__ = cbook.dedent(hist.__doc__) % martist.kwdocd
 
     def psd(self, x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to