Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## [Matplotlib-users] Matrix Structure Plots

 [Matplotlib-users] Matrix Structure Plots From: Nils Wagner - 2006-04-27 07:51:41 ```Hi all, What is the state-of-the-art of making matrix structure plots of sparse matrices with matplotlib ? Nils http://math.nist.gov/MatrixMarket/structureplots.html ```

 [Matplotlib-users] Matrix Structure Plots From: Nils Wagner - 2006-04-27 07:51:41 ```Hi all, What is the state-of-the-art of making matrix structure plots of sparse matrices with matplotlib ? Nils http://math.nist.gov/MatrixMarket/structureplots.html ```
 Re: [Matplotlib-users] Matrix Structure Plots From: John Pye - 2006-04-27 08:48:06 ```Hi Nils, You've got the 'spy' function, just like in MATLAB. Works a treat. For example, see under ' Viewing an incidence matrix' on the page: https://pse.cheme.cmu.edu/wiki/view/Ascend/ScreenShots In fact this example uses some tricks to perform colouring of the plot. All is explained in the matplotlib wiki. Cheers JP Nils Wagner wrote: > Hi all, > > What is the state-of-the-art of making matrix structure plots of sparse > matrices with matplotlib ? > > Nils > > http://math.nist.gov/MatrixMarket/structureplots.html -- John Pye Department of Mechanical and Manufacturing Engineering University of New South Wales, Sydney, Australia http://pye.dyndns.org/ ```
 Re: [Matplotlib-users] Matrix Structure Plots From: Nils Wagner - 2006-04-27 10:59:45 Attachments: structure.png ```John Pye wrote: > Hi Nils, > > You've got the 'spy' function, just like in MATLAB. Works a treat. For > example, see under ' Viewing an incidence matrix' on the page: > https://pse.cheme.cmu.edu/wiki/view/Ascend/ScreenShots > > In fact this example uses some tricks to perform colouring of the > plot. All is explained in the matplotlib wiki. > > Cheers > JP > > Nils Wagner wrote: >> Hi all, >> >> What is the state-of-the-art of making matrix structure plots of sparse >> matrices with matplotlib ? >> >> Nils >> >> http://math.nist.gov/MatrixMarket/structureplots.html Thank you for your feedback. Finally I have used Robert Cimrman's code to generate a matrix structure plot (structure.png). However, the entries should lie on the main diagonal instead of the antidiagonal. So what is missing in the code below ? How do I change the orientation of the plots ? from pylab import show, plot, axis, xlabel, subplot, title from scipy import * K = io.mmread('k.mtx') M = io.mmread('m.mtx') print 'Reading finished' rowk = K.row colk = K.col nItemk = len(colk) nRowk, nColk = shape(K) rowm = M.row colm = M.col nItemm = len(colm) nRowm, nColm = shape(M) subplot(211) title('Stiffness matrix') plot( colk + 0.5, rowk + 0.5, linestyle = 'None', marker = ',', markersize = 0.5, markeredgewidth = 0.1 ) axis( [-0.5, nRowk+0.5, -0.5, nColk+0.5] ) axis( 'equal' ) xlabel( '%d x %d: %d nnz, %.2f%% fill' % (nRowk, nColk, nItemk, 100. * nItemk / float( nRowk * nColk )) ) subplot(212) title('Mass matrix') plot( colm + 0.5, rowm + 0.5, linestyle = 'None', marker = ',', markersize = 0.5, markeredgewidth = 0.1 ) axis( [-0.5, nRowm+0.5, -0.5, nColm+0.5] ) axis( 'equal' ) xlabel( '%d x %d: %d nnz, %.2f%% fill' % (nRowm, nColk, nItemm, 100. * nItemm / float( nRowm * nColm )) ) show() ```
 Re: [Matplotlib-users] Matrix Structure Plots From: John Pye - 2006-04-27 12:21:20 Attachments: Message as HTML     matplotlib-spy.png ``` Hi Nils,

I think you're missing the simplicity of pylab if you don't use the 'spy' or 'spy2' approach. Here is what I had to do. (I needed to install the suggested http://matplotlib.sourceforge.net/matplotlibrc;, changing 'backend:TkAgg' and 'interactive:True')

Nils Wagner wrote:
Thank you for your feedback. Finally I have used Robert Cimrman's code to generate a matrix structure plot (structure.png).

--  John Pye Department of Mechanical and Manufacturing Engineering University of New South Wales, Sydney, Australia http://pye.dyndns.org/
; ```
 Re: [Matplotlib-users] Matrix Structure Plots From: Robert Cimrman - 2006-04-27 09:15:43 ```Nils Wagner wrote: > Hi all, > > What is the state-of-the-art of making matrix structure plots of sparse > matrices with matplotlib ? > > Nils > > http://math.nist.gov/MatrixMarket/structureplots.html I use this simple code: row, col are arrays of indices of nonzero matrix entries nItem long, nRow, nCol is the matrix shape plot( col + 0.5, row + 0.5, linestyle = 'None', marker = ',', markersize = 0.5, markeredgewidth = 0.1 ) axis( [-0.5, nRow+0.5, -0.5, nCol+0.5] ) axis( 'equal' ) xlabel( '%d x %d: %d nnz, %.2f%% fill' % (nRow, nCol, nItem, 100. * nItem / float( nRow * nCol )) ) In SciPy, to get row, col, convert your matrix to the 'COO' format via m = m.tocoo(), then m.row, m.col are your indices. r. ```
 Re: [Matplotlib-users] Matrix Structure Plots From: John Hunter - 2006-04-27 13:16:10 ```>>>>> "Nils" == Nils Wagner writes: Nils> Hi all, What is the state-of-the-art of making matrix Nils> structure plots of sparse matrices with matplotlib ? The irony of this question is that you asked the same question in 2004 and I implemented spy and spy2 in answer: http://sourceforge.net/mailarchive/message.php?msg_id=10020115 http://sourceforge.net/mailarchive/message.php?msg_id=10020116 JDH ```
 Re: [Matplotlib-users] Matrix Structure Plots From: Robert Cimrman - 2006-04-27 13:30:53 ```John Hunter wrote: >>>>>>"Nils" == Nils Wagner writes: > > > Nils> Hi all, What is the state-of-the-art of making matrix > Nils> structure plots of sparse matrices with matplotlib ? > > The irony of this question is that you asked the same question in > 2004 and I implemented spy and spy2 in answer: > > http://sourceforge.net/mailarchive/message.php?msg_id=10020115 > http://sourceforge.net/mailarchive/message.php?msg_id=10020116 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 :-) r. ```
 Re: [Matplotlib-users] Matrix Structure Plots From: John Hunter - 2006-04-27 13:35:13 ```>>>>> "Robert" == Robert Cimrman writes: Robert> Does it work for SciPy sparse matrices Robert> (e.g. scipy.sparse.csr_matrix)? I don't think so... That Robert> is why I provided my solution. Otherwise, of course, I am Robert> well aware of spy, spy2 :-) 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. JDH ```
 Re: [Matplotlib-users] Matrix Structure Plots From: Robert Cimrman - 2006-04-27 13:44:04 ```John Hunter wrote: >>>>>>"Robert" == Robert Cimrman writes: > > > Robert> Does it work for SciPy sparse matrices > Robert> (e.g. scipy.sparse.csr_matrix)? I don't think so... That > Robert> is why I provided my solution. Otherwise, of course, I am > Robert> well aware of spy, spy2 :-) > > 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... In [14]:import scipy.sparse as sp In [15]:import scipy as nm In [16]:a = nm.array( [[1, 0, 2, 0, 3]] ) In [17]:b = sp.csr_matrix( a ) In [18]:b Out[18]: <1x5 sparse matrix of type '' with 3 stored elements (space for 3) in Compressed Sparse Row format> In [19]:p b (0, 0) 1.0 (0, 2) 2.0 (0, 4) 3.0 In [20]:spy(b ) --------------------------------------------------------------------------- exceptions.TypeError Traceback (most recent call last) /home/eldaran/ /usr/lib/python2.4/site-packages/matplotlib/pylab.py in spy(*args, **kwargs) 2184 hold(h) 2185 try: -> 2186 ret = gca().spy(*args, **kwargs) 2187 draw_if_interactive() 2188 except: /usr/lib/python2.4/site-packages/matplotlib/axes.py in spy(self, Z, marker, markersize, **kwargs) 3557 The line handles are returned 3558 """ -> 3559 x,y,z = matplotlib.mlab.get_xyz_where(Z, Z>0) 3560 return self.plot(x+0.5,y+0.5, linestyle='None', 3561 marker=marker,markersize=markersize, **kwargs) /usr/lib/python2.4/site-packages/scipy/sparse/sparse.py in __cmp__(self, other) TypeError: comparison of sparse matrices not implemented In [23]:spy( a ) Out[23]:[] ```
 Re: [Matplotlib-users] Matrix Structure Plots From: John Hunter - 2006-04-27 14:55:43 ```>>>>> "Robert" == Robert Cimrman writes: Robert> John Hunter wrote: >>>>>>> "Robert" == Robert Cimrman writes: Robert> Does it work for SciPy sparse matrices Robert> (e.g. scipy.sparse.csr_matrix)? I don't think so... That Robert> is why I provided my solution. Otherwise, of course, I am Robert> well aware of spy, spy2 :-) >> 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. Robert> Sure: Robert> matplotlib: 0.87.2-r2 Robert> from SVN: numpy: Checked out revision 2433. scipy: Robert> Checked out revision 1888. Robert> The problem is, that you cannot use 'where' for sparse Robert> 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 = c.data else: 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 axes.py 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.. JDH ```