Revision: 6743
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6743&view=rev
Author:   jdh2358
Date:     2009-01-06 17:06:06 +0000 (Tue, 06 Jan 2009)

Log Message:
-----------
added marginal density option to hexbin

Modified Paths:
--------------
    trunk/matplotlib/CHANGELOG
    trunk/matplotlib/boilerplate.py
    trunk/matplotlib/examples/pylab_examples/hexbin_demo2.py
    trunk/matplotlib/lib/matplotlib/axes.py
    trunk/matplotlib/setupext.py

Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG  2009-01-06 16:53:09 UTC (rev 6742)
+++ trunk/matplotlib/CHANGELOG  2009-01-06 17:06:06 UTC (rev 6743)
@@ -1,3 +1,6 @@
+2009-01-06 Added marginals kwarg to hexbin to plot marginal densities 
+           JDH
+
 2009-01-06 Change user-visible multipage pdf object to PdfPages to
            avoid accidents with the file-like PdfFile. - JKS
 

Modified: trunk/matplotlib/boilerplate.py
===================================================================
--- trunk/matplotlib/boilerplate.py     2009-01-06 16:53:09 UTC (rev 6742)
+++ trunk/matplotlib/boilerplate.py     2009-01-06 17:06:06 UTC (rev 6743)
@@ -102,7 +102,7 @@
 cmappable = {
     'contour' : 'if ret._A is not None: gci._current = ret',
     'contourf': 'if ret._A is not None: gci._current = ret',
-    'hexbin' : 'gci._current = ret',
+    'hexbin' : 'gci._current = ret[0]',
     'scatter' : 'gci._current = ret',
     'pcolor'  : 'gci._current = ret',
     'pcolormesh'  : 'gci._current = ret',

Modified: trunk/matplotlib/examples/pylab_examples/hexbin_demo2.py
===================================================================
--- trunk/matplotlib/examples/pylab_examples/hexbin_demo2.py    2009-01-06 
16:53:09 UTC (rev 6742)
+++ trunk/matplotlib/examples/pylab_examples/hexbin_demo2.py    2009-01-06 
17:06:06 UTC (rev 6743)
@@ -39,13 +39,14 @@
 gridsize=30
 
 plt.subplot(211)
-plt.hexbin(x,y, C=z, gridsize=gridsize )
+plt.hexbin(x,y, C=z, gridsize=gridsize, marginals=True)
 plt.axis([xmin, xmax, ymin, ymax])
 cb = plt.colorbar()
 cb.set_label('mean value')
 
+
 plt.subplot(212)
-plt.hexbin(x,y, gridsize=gridsize )
+plt.hexbin(x,y, gridsize=gridsize)
 plt.axis([xmin, xmax, ymin, ymax])
 cb = plt.colorbar()
 cb.set_label('N observations')

Modified: trunk/matplotlib/lib/matplotlib/axes.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/axes.py     2009-01-06 16:53:09 UTC (rev 
6742)
+++ trunk/matplotlib/lib/matplotlib/axes.py     2009-01-06 17:06:06 UTC (rev 
6743)
@@ -5212,7 +5212,7 @@
                     xscale = 'linear', yscale = 'linear',
                     cmap=None, norm=None, vmin=None, vmax=None,
                     alpha=1.0, linewidths=None, edgecolors='none',
-                    reduce_C_function = np.mean, mincnt=None,
+                    reduce_C_function = np.mean, mincnt=None, marginals=True,
                     **kwargs):
         """
         call signature::
@@ -5221,7 +5221,7 @@
                  xscale = 'linear', yscale = 'linear',
                  cmap=None, norm=None, vmin=None, vmax=None,
                  alpha=1.0, linewidths=None, edgecolors='none'
-                 reduce_C_function = np.mean, mincnt=None,
+                 reduce_C_function = np.mean, mincnt=None, marginals=True
                  **kwargs)
 
         Make a hexagonal binning plot of *x* versus *y*, where *x*,
@@ -5273,6 +5273,11 @@
             If not None, only display cells with more than *mincnt*
             number of points in the cell
 
+          *marginals*: True|False
+            if marginals is True, plot the marginal density as
+            colormapped rectagles along the bottom of the x-axis and
+            left of the y-axis
+
         Other keyword arguments controlling color mapping and normalization
         arguments:
 
@@ -5320,8 +5325,11 @@
         :class:`~matplotlib.collections.PolyCollection` instance; use
         :meth:`~matplotlib.collection.PolyCollection.get_array` on
         this :class:`~matplotlib.collections.PolyCollection` to get
-        the counts in each hexagon.
+        the counts in each hexagon..  If marginals is True, horizontal
+        bar and vertical bar (both PolyCollections) will be attached
+        to the return collection as attributes *hbar* and *vbar*
 
+
         **Example:**
 
         .. plot:: mpl_examples/pylab_examples/hexbin_demo.py
@@ -5331,8 +5339,10 @@
 
         self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
 
+        
         x, y, C = cbook.delete_masked_points(x, y, C)
 
+            
         # Set the size of the hexagon grid
         if iterable(gridsize):
             nx, ny = gridsize
@@ -5357,6 +5367,11 @@
         xmax += padding
         sx = (xmax-xmin) / nx
         sy = (ymax-ymin) / ny
+
+        if marginals:
+            xorig = x.copy()
+            yorig = y.copy()
+
         x = (x-xmin)/sx
         y = (y-ymin)/sy
         ix1 = np.round(x).astype(int)
@@ -5496,6 +5511,93 @@
 
         # add the collection last
         self.add_collection(collection)
+        if not marginals:
+            return collection
+
+
+        if C is None:
+            C = np.ones(len(x))
+
+        def coarse_bin(x, y, coarse):
+            ind = coarse.searchsorted(x).clip(0, len(coarse)-1)
+            mus = np.zeros(len(coarse))
+            for i in range(len(coarse)):
+                mu = reduce_C_function(y[ind==i])
+                mus[i] = mu
+            return mus
+
+        coarse = np.linspace(xmin, xmax, gridsize)
+
+        xcoarse = coarse_bin(xorig, C, coarse)
+        valid = ~np.isnan(xcoarse)
+        verts, values = [], []
+        for i,val in enumerate(xcoarse):
+            thismin = coarse[i]
+            if i<len(coarse)-1:
+                thismax = coarse[i+1]
+            else:
+                thismax = thismin + np.diff(coarse)[-1]
+
+            if not valid[i]: continue                
+
+            verts.append([(thismin, 0), (thismin, 0.05), (thismax, 0.05), 
(thismax, 0)])
+            values.append(val)
+
+        values = np.array(values)
+        trans = mtransforms.blended_transform_factory(
+            self.transData, self.transAxes)
+
+
+        hbar = mcoll.PolyCollection(verts, transform=trans, edgecolors='face')
+
+        hbar.set_array(values)
+        hbar.set_cmap(cmap)
+        hbar.set_norm(norm)
+        hbar.set_alpha(alpha)
+        hbar.update(kwargs)
+        self.add_collection(hbar)
+
+        coarse = np.linspace(ymin, ymax, gridsize)
+        ycoarse = coarse_bin(yorig, C, coarse)
+        valid = ~np.isnan(ycoarse)
+        verts, values = [], []
+        for i,val in enumerate(ycoarse):
+            thismin = coarse[i]
+            if i<len(coarse)-1:
+                thismax = coarse[i+1]
+            else:
+                thismax = thismin + np.diff(coarse)[-1]
+            if not valid[i]: continue
+            verts.append([(0, thismin), (0.0, thismax), (0.05, thismax), 
(0.05, thismin)])
+            values.append(val)
+
+        values = np.array(values)
+
+
+        trans = mtransforms.blended_transform_factory(
+            self.transAxes, self.transData)
+
+        vbar = mcoll.PolyCollection(verts, transform=trans, edgecolors='face')
+        vbar.set_array(values)
+        vbar.set_cmap(cmap)
+        vbar.set_norm(norm)
+        vbar.set_alpha(alpha)
+        vbar.update(kwargs)
+        self.add_collection(vbar)
+
+
+
+        collection.hbar = hbar
+        collection.vbar = vbar
+
+        def on_changed(collection):
+            hbar.set_cmap(collection.get_cmap())
+            hbar.set_clim(collection.get_clim())
+            vbar.set_cmap(collection.get_cmap())
+            vbar.set_clim(collection.get_clim())
+
+        collection.callbacksSM.connect('changed', on_changed)
+
         return collection
 
     hexbin.__doc__ = cbook.dedent(hexbin.__doc__) % martist.kwdocd

Modified: trunk/matplotlib/setupext.py
===================================================================
--- trunk/matplotlib/setupext.py        2009-01-06 16:53:09 UTC (rev 6742)
+++ trunk/matplotlib/setupext.py        2009-01-06 17:06:06 UTC (rev 6743)
@@ -1066,8 +1066,7 @@
     global BUILT_WINDOWING
     if BUILT_WINDOWING: return # only build it if you you haven't already
     module = Extension('matplotlib._windowing',
-                       ['src/_windowing.cpp',
-                        ],
+                       ['src/_windowing.cpp'],
                        )
     add_windowing_flags(module)
     ext_modules.append(module)
@@ -1341,7 +1340,7 @@
     temp_copy('src/_backend_gdk.c', 'src/backend_gdk.c')
     module = Extension(
         'matplotlib.backends._backend_gdk',
-        ['src/backend_gdk.c', ],
+        ['src/backend_gdk.c'],
         libraries = [],
         include_dirs=numpy_inc_dirs,
         )


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to