Robert and any other spy users:

I have committed to svn a new axes method, spy3, that combines a modification of the functionality of both spy and spy2. I hope you can check it out. If it looks OK, then I would like to simply replace spy and spy2 with this combined version. In that case the pylab interface, which now gives access to spy and spy2, would have a single spy function which would access the new version. My suspicion is that spy is used almost entirely in interactive mode, and probably not by very many people, so that this changeover can be made quickly with little disruption to present users.

Attached is a script illustrating the difference in the way spy3 displays a matrix (it matches the way it is printed: first index is row number, second is column number, row number increases down) versus what spy and spy2 do (first index is X, second index is Y).

Also attached is the diff for spy3.

Users may want to comment in particular on the default for the "aspect" kwarg. Presently it is set to "equal" so that the shape of the plot is the shape of the array with square cells. This differs from spy and spy2. The rationale is that it gives the best picture of what the array looks like, including its shape.

Eric






Robert Cimrman wrote:
Eric Firing wrote:
One more miscellaneous thought: perhaps spy and spy2 should be consolidated into a single function with a kwarg to select the marker version or the image version? Their purpose is identical (isn't it?), and it would reduce namespace clutter.

one more thing here: usually (e.g. in Matlab) the y axis is reversed, so
that one sees the sparsity pattern in the same position as one would see
on paper when writing down the corresponding system of linear equations.
(I use my own spy, with ax.set_ylim( ax.get_ylim()[::-1] ) for this
purpose.)

just my 2 cents, in case of the consolidation,
r.


from pylab import *

xx = zeros((5,9))
xx[0,0] = 1
xx[0,3] = 1
xx[3,6] = 1

print xx

ax1 = subplot(2,2,1)
ax1.spy(xx)
ax1.set_title('spy(xx)')

ax2 = subplot(2,2,2)
ax2.spy2(xx)
ax2.set_title('spy2(xx)')

ax3 = subplot(2,2,3)
ax3.spy3(xx, marker='s')
ax3.set_title("spy3(xx, marker='s')")

ax4 = subplot(2,2,4)
ax4.spy3(xx)
ax4.set_title('spy3(xx)')
show()
Index: lib/matplotlib/axes.py
===================================================================
--- lib/matplotlib/axes.py	(revision 2901)
+++ lib/matplotlib/axes.py	(revision 2902)
@@ -19,7 +19,7 @@
 from collections import RegularPolyCollection, PolyCollection, LineCollection, \
      QuadMesh, StarPolygonCollection, BrokenBarHCollection
 from colors import colorConverter, normalize, Colormap, \
-        LinearSegmentedColormap, looks_like_color, is_color_like
+        LinearSegmentedColormap, ListedColormap, looks_like_color, is_color_like
 import cm
 from cm import ScalarMappable
 from contour import ContourSet
@@ -4394,8 +4394,83 @@
 
         return self.imshow(transpose(Z), interpolation='nearest', **kwargs)
 
+    def spy3(self, Z, precision=None, marker=None, markersize=None,
+                                    aspect='equal', **kwargs):
+        """
+        spy(Z) plots the sparsity pattern of the 2-D array Z
 
+        If precision is None, any non-zero value will be plotted;
+        else, values of absolute(Z)>precision will be plotted.
 
+        The array will be plotted as it would be printed, with
+        the first index (row) increasing down and the second
+        index (column) increasing to the right.
+
+        By default aspect is 'equal' so that each array element
+        occupies a square space; set the aspect kwarg to 'auto'
+        to allow the plot to fill the plot box, or to any scalar
+        number to specify the aspect ratio of an array element
+        directly.
+
+        If marker and markersize are None, an image will be
+        returned and any remaining kwargs are passed to imshow;
+        else, a Line2D object will be returned with the value
+        of marker determining the marker type, and any remaining
+        kwargs passed to the axes plot method.
+
+        If marker and markersize are None, useful kwargs include:
+            cmap
+            alpha
+        See documentation for imshow() for details.
+        For controlling colors, e.g. cyan background and red marks, use:
+            cmap = matplotlib.colors.ListedColormap(['c','r'])
+
+        If marker or markersize is not None, useful kwargs include:
+            marker
+            markersize
+            color
+        See documentation for plot() for details.
+
+        Useful values for marker include:
+            's'  square (default)
+            'o'  circle
+            '.'  point
+            ','  pixel
+
+        """
+        if marker is None and markersize is None:
+            Z = asarray(Z)
+            if precision is None: mask = Z!=0.
+            else:                 mask = absolute(Z)>precision
+
+            if 'cmap' not in kwargs:
+                kwargs['cmap'] = ListedColormap(['w', 'k'], name='binary')
+            nr, nc = Z.shape
+            extent = [-0.5, nc-0.5, nr-0.5, -0.5]
+            return self.imshow(mask, interpolation='nearest', aspect=aspect,
+                                extent=extent, origin='upper', **kwargs)
+        else:
+            if hasattr(Z, 'tocoo'):
+                c = Z.tocoo()
+                y = c.row
+                x = c.col
+                z = c.data
+            else:
+                Z = asarray(Z)
+                if precision is None: mask = Z!=0.
+                else:                 mask = absolute(Z)>precision
+                y,x,z = matplotlib.mlab.get_xyz_where(mask, mask)
+            if marker is None: marker = 's'
+            if markersize is None: markersize = 10
+            lines = self.plot(x, y, linestyle='None',
+                         marker=marker, markersize=markersize, **kwargs)
+            nr, nc = Z.shape
+            self.set_xlim(xmin=-0.5, xmax=nc-0.5)
+            self.set_ylim(ymin=nr-0.5, ymax=-0.5)
+            self.set_aspect(aspect)
+            return lines
+
+
 class SubplotBase:
     """
     Emulate matlab's(TM) subplot command, creating axes with
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to