I just spent a little time getting hexbin to discard nans without
failing in mysterious ways.

I'm sending 2 patches: one will detect nans and raise a suitable
exception. The other will automatically drop the nans and continue with
hexbin. The 2nd seems a little nicer in functionality, but the code is a
little more convoluted and hence more brittle, and so I'm torn as to
which I'd prefer. (The problem with just detecting nans from the
beginning is that it would cause an extra pass through the data in all
cases.)

Anyhow, I've attached them both here for discussion. I'm happy to check
one of these in myself or feel free to do it.

-Andrew
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py	(revision 5789)
+++ lib/matplotlib/axes.py	(working copy)
@@ -5106,6 +5106,8 @@
         xmax = np.amax(x)
         ymin = np.amin(y)
         ymax = np.amax(y)
+        if np.any(np.isnan((xmin,xmax,ymin,ymax))):
+            raise ValueError('input arrays cannot have nan')
         # In the x-direction, the hexagons exactly cover the region from
         # xmin to xmax. Need some padding to avoid roundoff errors.
         padding = 1.e-9 * (xmax - xmin)
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py	(revision 5789)
+++ lib/matplotlib/axes.py	(working copy)
@@ -5099,13 +5099,33 @@
         x = np.array(x, float)
         y = np.array(y, float)
         if xscale=='log':
+            orig_x = x
             x = np.log10(x)
         if yscale=='log':
+            orig_y = y
             y = np.log10(y)
         xmin = np.amin(x)
         xmax = np.amax(x)
         ymin = np.amin(y)
         ymax = np.amax(y)
+
+        # This allows us to detect nans and re-start without making an
+        # additional pass through the data. (Ideally, we would pick
+        # this up in delete_masked_points(), but that would require an
+        # additional loop through the arrays.)
+        if np.any(np.isnan((xmin,xmax,ymin,ymax))):
+            if xscale=='log':
+                x = orig_x # reverse x scaling done above
+            if yscale=='log':
+                y = orig_y # reverse y scaling done above
+            x = np.ma.masked_where( np.isnan(x), x)
+            y = np.ma.masked_where( np.isnan(y), y)
+            return self.hexbin(x, y, gridsize = gridsize, bins = bins,
+                               xscale = xscale, yscale = yscale,
+                               cmap=cmap, norm=norm, vmin=vmin, vmax=vmax,
+                               alpha=alpha, linewidths=linewidths, edgecolors=edgecolors,
+                               **kwargs)
+
         # In the x-direction, the hexagons exactly cover the region from
         # xmin to xmax. Need some padding to avoid roundoff errors.
         padding = 1.e-9 * (xmax - xmin)
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to