Matrix Structure Plots

John Hunter wrote:

    > Does it work for SciPy sparse matrices
    > (e.g. scipy.sparse.csr_matrix)? I don't think so... That
    > is why I provided my solution. Otherwise, of course, I am
    > well aware of spy, spy2 :slight_smile:
    >> I see -- can you post a simple example (with version nums for
    >> numpy/scipy/mpl) that exposes the bug. Hopefully there will be
    >> an easy fix.

    > Sure:

    > matplotlib: 0.87.2-r2

    > from SVN: numpy: Checked out revision 2433. scipy:
    > Checked out revision 1888.

    > The problem is, that you cannot use 'where' for sparse
    > matrices yet...

For spy we can use

    def spy(self, Z, marker='s', markersize=10, **kwargs):
        SPY(Z, **kwargs) plots the sparsity pattern of the matrix Z
        using plot markers.

        kwargs give the marker properties - see help(plot) for more
        information on marker properties

        The line handles are returned
        if hasattr(Z, 'tocoo'):
            c = Z.tocoo()
            x = c.row
            y = c.col
            z =
            x,y,z = matplotlib.mlab.get_xyz_where(Z, Z>0)
        return self.plot(x+0.5,y+0.5, linestyle='None',
                         marker=marker,markersize=markersize, **kwargs)

you may want to plug this into your and test.

For spy2 it is a bit tricker, since it uses an image. One option
would be to create a regular array of dimensions MxN and fill in the
nonempty cells, but this kind of defeats the purpose of using sparse
arrays. Another is to use a special purpose RegularPolygonCollection,
and build rectangles at each non-zero pixel. I like this option best
because it preserves sparsity and allows colormapping, etc..