matshow with sparse matrices

How can you visualize the sparsity pattern of a large sparse matrix?

The matrix is too large to fit in memory as a dense array, so I have it in csr_matrix format. When I try pylab’s matshow with it, I get the following error:

ValueError: need more than 0 values to unpack



import pylab as pl
import scipy.sparse as sp
from random import randint

mat = sp.lil_matrix( (4000,3000), dtype='uint8' )
for i in range(1000):
    mat[randint(0,4000),randint(0,3000)] = randint(0,10)


Best answer

matshow works on dense arrays. For sparse arrays you can use spy:

import scipy.sparse as sps
import matplotlib.pyplot as plt

a = sps.rand(1000, 1000, density=0.001, format='csr')


enter image description here