subplots of matshow

I am trying to create subplots with matshow. However, my code seems to be displaying only the last matshow image and two blank plots in separate figures. Can someone tell me what I am doing wrong?

from matplotlib.pylab import *

def SliceMat(N, i):
    slice = zeros( (N, N) )
    for j in range(N):
        for k in range(N):
            val = 0 # black
            if(i>j>k and ((i+j+k)%3==1)):
                val=1 # red
            if(j>k>i and ((i+j+k)%3==2)):
                val=2 # yellow
            if (k>i>j and ((i+j+k)%3==0)):
                val=3 # white
            slice[j,k] = val
    return slice

N=8

subplot(121)
slice = SliceMat(N, 2)
matshow(slice, cmap=cm.hot, origin='lower')

subplot(122)
slice = SliceMat(N, 5)
matshow(slice, cmap=cm.hot, origin='lower')

show()

···

_________________________________________________________________
Connect with your friends who use Yahoo! Messenger with Voice. Click! http://www.msnspecials.in/wlmyahoo/index.asp

def matshow(*args,**kw):
     """Display an array as a matrix in a new figure window.

Matshow simply is not designed to work with subplots; it is a wrapper around imshow() that creates a new figure. You should be able to do what you want by using imshow() directly in place of matshow with something like this:

nr, nc = Z.shape
extent = [-0.5, nc-0.5, nr-0.5, -0.5]
imshow(Z, extent=extent, origin='upper')

This should display the matrix with the same orientation as it has when printed, and with the axis ticks matching the indices.

Change or add kwargs to imshow as needed. aspect is 'equal' by default, which may or may not be what you want, but it is part of what matshow tries to do if the matrix aspect ratio is not too far from unity.

Eric

TheSaint 555 wrote:

···

I am trying to create subplots with matshow. However, my code seems to be displaying only the last matshow image and two blank plots in separate figures. Can someone tell me what I am doing wrong?

from matplotlib.pylab import *

def SliceMat(N, i):
    slice = zeros( (N, N) )
    for j in range(N):
        for k in range(N):
            val = 0 # black
            if(i>j>k and ((i+j+k)%3==1)):
                val=1 # red
            if(j>k>i and ((i+j+k)%3==2)):
                val=2 # yellow
            if (k>i>j and ((i+j+k)%3==0)):
                val=3 # white
            slice[j,k] = val
    return slice

N=8

subplot(121)
slice = SliceMat(N, 2)
matshow(slice, cmap=cm.hot, origin='lower')

subplot(122)
slice = SliceMat(N, 5)
matshow(slice, cmap=cm.hot, origin='lower')

show()

_________________________________________________________________
Connect with your friends who use Yahoo! Messenger with Voice. Click! http://www.msnspecials.in/wlmyahoo/index.asp

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

That worked. Thanx a lot Eric. I just had to also add interpolation='nearest' in imshow() and it worked just like matshow did.

···

From: Eric Firing <efiring@...202...>
To: TheSaint 555 <thesaint555@...32...>
CC: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] subplots of matshow
Date: Mon, 04 Dec 2006 14:42:20 -1000

def matshow(*args,**kw):
    """Display an array as a matrix in a new figure window.

Matshow simply is not designed to work with subplots; it is a wrapper around imshow() that creates a new figure. You should be able to do what you want by using imshow() directly in place of matshow with something like this:

nr, nc = Z.shape
extent = [-0.5, nc-0.5, nr-0.5, -0.5]
imshow(Z, extent=extent, origin='upper')

This should display the matrix with the same orientation as it has when printed, and with the axis ticks matching the indices.

Change or add kwargs to imshow as needed. aspect is 'equal' by default, which may or may not be what you want, but it is part of what matshow tries to do if the matrix aspect ratio is not too far from unity.

Eric

TheSaint 555 wrote:

I am trying to create subplots with matshow. However, my code seems to be displaying only the last matshow image and two blank plots in separate figures. Can someone tell me what I am doing wrong?

from matplotlib.pylab import *

def SliceMat(N, i):
    slice = zeros( (N, N) )
    for j in range(N):
        for k in range(N):
            val = 0 # black
            if(i>j>k and ((i+j+k)%3==1)):
                val=1 # red
            if(j>k>i and ((i+j+k)%3==2)):
                val=2 # yellow
            if (k>i>j and ((i+j+k)%3==0)):
                val=3 # white
            slice[j,k] = val
    return slice

N=8

subplot(121)
slice = SliceMat(N, 2)
matshow(slice, cmap=cm.hot, origin='lower')

subplot(122)
slice = SliceMat(N, 5)
matshow(slice, cmap=cm.hot, origin='lower')

show()

_________________________________________________________________
Connect with your friends who use Yahoo! Messenger with Voice. Click! http://www.msnspecials.in/wlmyahoo/index.asp

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

_________________________________________________________________
Get up-to-date with movies, music and TV. Its happening on MSN Entertainment http://content.msn.co.in/Entertainment/Default