15

I have been porting code for an isomap algorithm from MATLAB to Python. I am trying to visualize the sparsity pattern using the spy function.

MATLAB command:

spy(sparse(A));
drawnow;

Python command:

matplotlib.pyplot.spy(scipy.sparse.csr_matrix(A))
plt.show()

I am not able to reproduce the MATLAB result in Python using the above command. Using the command with only A in non-sparse format gives quite similar result to MATLAB. But it's taking quite long (A being 2000-by-2000). What would be the MATLAB equivalent of a sparse function for scipy?

Jan
  • 4,932
  • 1
  • 26
  • 30
Nitin
  • 2,624
  • 7
  • 29
  • 43

4 Answers4

19

Maybe it's your version of matplotlib that makes trouble, as for me scipy.sparse and matplotlib.pylab work well together.

See sample code below that produces the 'spy' plot attached.

import matplotlib.pylab as plt
import scipy.sparse as sps
A = sps.rand(10000,10000, density=0.00001)
M = sps.csr_matrix(A)
plt.spy(M)
plt.show()

# Returns here '1.3.0'
matplotlib.__version__

This gives this plot: enter image description here

Community
  • 1
  • 1
Jan
  • 4,932
  • 1
  • 26
  • 30
  • Thanks. I figured out that the error was in my code for creating the A matrix in Python.One quick question. The blue squares produced are the non-zeros right? – Nitin Sep 06 '13 at 19:34
7

I just released betterspy, which arguably does a better job here. Install with

pip install betterspy

and run with

import betterspy
from scipy import sparse

A = sparse.rand(20, 20, density=0.1)
betterspy.show(A)
betterspy.write_png("out.png", A)

enter image description here

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • looks like a nice complement for matplotlib. Is it possible to use axis properties, such as, `ax.set_xticklabels(...)` for `betterspy.show()`? Nice job by the way. – Traxidus Wolf Jul 01 '19 at 04:08
  • Simply use `betterspy.plot(A)`, set whatever you want on `gca()`, and then `pyplot.show()` the image. – Nico Schlömer Jul 01 '19 at 12:42
  • Oh boi, works like a charm. Didn't saw that method on readme file. I suggest and improvement on documentation. Thanks. @Nitin this should be the accepted answer. Here a quick example: `import matplotlib.pyplot as plt, betterspy as bspy, scipy.sparse as sps; A=sps.rand(5,5, density=.4); bspy.plot(A); ax=plt.gca(); ax.set_xticklabels(['','a','b','c','d','e']); plt.show()` – Traxidus Wolf Jul 03 '19 at 01:18
5

With smaller markers:

import matplotlib.pylab as pl
import scipy.sparse as sps
import scipy.io
import sys
A=scipy.io.mmread(sys.argv[1])
pl.spy(A,precision=0.01, markersize=1)
pl.show()
Kadir
  • 1,345
  • 3
  • 15
  • 25
0

See matspy.

pip install matspy

Large matrices are no problem, a spy plot of tens of millions of nonzeros takes less than half a second. A small example:

from matspy import spy
import scipy

n = 9000
A = scipy.sparse.random(n, n, density=0.001) + scipy.sparse.eye(n)

spy(A)

matspy output

If you want to modify the plot further, use fig, ax = matspy.spy_to_mpl(A) instead.

Adam
  • 16,808
  • 7
  • 52
  • 98