Feature request : Comparison of sparse matrices is not implemented. (Nils Wagner)

Date: Thu, 02 Dec 2004 10:04:20 +0100
From: Nils Wagner <nwagner@...56...>
To: SciPy Developers List <scipy-dev@...29...>
Cc: matplotlib-users@lists.sourceforge.net
Subject: [Matplotlib-users] Feature request : Comparison of sparse matrices is not implemented.

Hi all,
I tried to visualize the structure of large and sparse matrices using

from matplotlib.colors import LinearSegmentedColormap
from matplotlib.matlab import *
from scipy import *
import IPython

def spy2(Z):
    """
    SPY(Z) plots the sparsity pattern of the matrix S as an image
    """

    #binary colormap min white, max black
    cmapdata = {
         'red' : ((0., 1., 1.), (1., 0., 0.)),
         'green': ((0., 1., 1.), (1., 0., 0.)),
         'blue' : ((0., 1., 1.), (1., 0., 0.))
         }
    binary = LinearSegmentedColormap('binary', cmapdata, 2)

    Z = where(Z>0,1.,0.)
    imshow(transpose(Z), interpolation='nearest', cmap=binary)

rows, cols, entries, rep, field, symm = io.mminfo('k0.mtx')
print 'number of rows, cols and entries', rows, cols, entries
print 'Start reading matrix - this may take a minute'
ma = io.mmread('k0.mtx')
print 'Finished'
flag = 1
if flag == 1:
spy2(ma)
show()

It failed. Is it somehow possible to visualize sparse matrices ?
Any suggestion would be appreciated.

To plot sparsity patterns, I read matrices in MatrixMarket format using the PySparse package. Once I have arrays with row and column indices, I simply use scatter() with arguments to modify the size of the 'bubbles' and their color (and alpha) depending on the magnitude on the nonzero element. The result is great. Of course when matrices are symmetric, you only hold the lower (or upper) triangular part, then tell scatter() to plot the other triangle.

Dominique