Routine to show matrices?

I just tested this code on a second box with different

    > mpl defaults and found a few minor issues. They are now
    > fixed in this new attached version.

Hi Fernando, this is a very useful submission that solves a recurrent
problem people have. So useful, that I factored out the hard part
(getting the figure size right) into a separate function figaspect
which lives in matplotlib.figure so it could be reused in other
contexts. The rest is easy, and lives in pylab.matshow, which calls
figaspect to do the heavy lifting.

A few comments

* you also need to be sure that the axes have the same width and
   height. Since the width and height are expressed in fractions of
   the figure size, you can use any width and height you want and get
   the right answer as long as they are the same, since the figure
   width and height have the right aspect ration. The default axes --
   subplot(111) -- have slightly different values for w and h. This
   is fixed in CVS with

    w,h = figaspect(arr)
    fig = figure(figsize=(w,h))
    ax = fig.add_axes([0.0, 0.05, 0.8, 0.8])

* To get the labels on top, you do

   ax.xaxis.tick_top() # this turns off tick bottom and turns on tick top

   likewise, there are yaxis functions tick_left and tick_right. This
   is also in CVS

  * I return a (fig, ax, im) tuple

Give it a test drive and let me know what you think (pylab revision
1.34 or later in CVS)

JDH

John Hunter wrote:

* you also need to be sure that the axes have the same width and
   height. Since the width and height are expressed in fractions of
   the figure size, you can use any width and height you want and get
   the right answer as long as they are the same, since the figure
   width and height have the right aspect ration. The default axes --
   subplot(111) -- have slightly different values for w and h. This
   is fixed in CVS with

    w,h = figaspect(arr)
    fig = figure(figsize=(w,h))
    ax = fig.add_axes([0.0, 0.05, 0.8, 0.8])

Well, but this breaks things badly. Have a run at my supplied examples with your version, and you'll see the arrays get banged against the left side. (make sure to rename the matshow routine in my example to matshow2, so you are actually testing matplotlib and not my code)

I played with these numbers a bit but couldn't get something that looked OK for all the examples. The original looked perfect, so it's a matter of reusing the same defaults that I was ending up with via my imshow() call, I guess. But I did some grepping for add_axes and couldn't find it elsewhere, so I don't know how to fix it.

* To get the labels on top, you do

   ax.xaxis.tick_top() # this turns off tick bottom and turns on tick top

   likewise, there are yaxis functions tick_left and tick_right. This
   is also in CVS

This is nice, thanks.

  * I return a (fig, ax, im) tuple

OK.

Minor docstring nits:

     matshow() calls imshow() with Aargs and **kwargs, but by default

should be:

     matshow() calls imshow() with *args and **kwargs, but by default

And why do you use enumerate here?

     for i, d in enumerate(dimlist):
         fig, ax, im = matshow(samplemat(d))
     show()

This example would be clearer with just

     for d dimlist:
         fig, ax, im = matshow(samplemat(d))
     show()

since you don't really use the index for anything. Less noise to read through.

Anyway, many thanks for including this. Let me know when you fix the current bugs and I'll test it again.

Cheers,

f