Monday, April 4, 2016

Matlab spy plot style in Python (sparse pattern)

The first time I used the command spy to show the sparse pattern of a sparse matrix, I wasn't satisfied with the result,
as a person who are used to Matlab plots. In this figure isn't easy to see the sparse pattern of the the matrix. After reading about the options available in the spy command, I found the option markersize. Changing the value of this parameter we can obtain a more familiar plot.
This is the code,
#!/usr/bin/env python
import scipy.sparse as sparse
import matplotlib.pyplot as plt
import as io
import numpy as np

#Downloading the matrix from SPARSEKIT
problem = "SPARSKIT/drivcav/e05r0200"
mm = np.lib._datasource.Repository('')
f ='%s.mtx.gz' % problem)
A = io.mmread(f).tocsr()
#Default Ploting
#Closer to Matlab's result


  1. Can you also crop the figure on the left and right?

  2. Perhaps even better: betterspy. (A small project of mine.)