imshow: axis not natural

I guess it was a FAQ :slight_smile:

The correct way - after experimenting with imshow options - is to transpose the data and use origin='lower' option

imshow(c.T,interpolation='nearest',origin='lower')

Piotr

路路路

----- Original Message ----

From: Piotr Zolnierczuk <zolnie@...9...>

To: matplotlib-users@lists.sourceforge.net

Sent: Friday, February 22, 2008 2:31:08 PM

Subject: [Matplotlib-users] imshow: axis not natural

Hi,

it is probably a faq - but I could not find it anywhere.

I would like to plot a 2D distribution (2D histogram) of some data using a 2D numpy.array that contains my data: c[ix,iy]

When I plot it via imshow the x axis points downwards and the y axis point to the left.

What I want is that the x-axis points to the left and the y axis points upwards.

So I ended up writing a small script twodimshow (below) where I transpose data (x<->y) and also flip the sign of the y-axis

Now: is there a better way of doing it?

See also CERN Root C++ package (ftp://root.cern.ch/root/doc/3Histograms.pdf) and/or (SLAC HIPPO (http://www.slac.stanford.edu/grp/ek/hippodraw/)

------------------- CODE BELOW ---------------------------

import numpy

from pylab import *

def convertToImShow(data,copy=True):

聽聽聽聽聽聽'''Converts data so that imshow displays x,y as a normal

聽聽聽聽聽聽physicist expects ie x pointing left and y pointing up'''

聽聽聽聽聽聽data = numpy.array(data.T,copy=copy)

聽聽聽聽聽聽if data.ndim != 2:

聽聽聽聽聽聽聽聽聽聽聽聽raise TypeError,'data must be a two-dimensional matrix'

聽聽聽聽聽聽nx,ny = data.shape

聽聽聽聽聽聽for i in range(nx/2):

聽聽聽聽聽聽聽聽聽聽聽聽j= nx - i - 1

聽聽聽聽聽聽聽聽聽聽聽聽for k in range(ny): data[j,k],data[i,k]=data[i,k],data[j,k]

聽聽聽聽聽聽return data

def twodimshow(data,extent=None,aspect=None):

聽聽聽聽聽聽r = convertToImShow(data)

聽聽聽聽聽聽if not aspect and extent:

聽聽聽聽聽聽聽聽聽聽聽聽if len(extent)>=4:

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽aspect = (extent[1] - extent[0])/float(extent[3] - extent[2])

聽聽聽聽聽聽imshow(r,extent=extent,aspect=aspect,interpolation='nearest')

if __name__ == "__main__":

聽聽聽聽聽聽c = zeros((4,4))

聽聽聽聽聽聽for ix in range(4):

聽聽聽聽聽聽聽聽聽聽聽聽for iy in range(ix):

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽c[ix,iy]=ix+iy

聽聽聽聽聽聽figure()

聽聽聽聽聽聽subplot(221)

聽聽聽聽聽聽imshow(c ,interpolation='nearest')

聽聽聽聽聽聽title('Axis direction not natural')

聽聽聽聽聽聽xlabel('Y-Axis'); ylabel('X-Axis')

聽聽聽聽聽聽subplot(222)

聽聽聽聽聽聽imshow(c.T ,interpolation='nearest')

聽聽聽聽聽聽title('Not there yet')

聽聽聽聽聽聽xlabel('X-Axis'); ylabel('Y-Axis')

聽聽聽聽聽聽subplot(223)

聽聽聽聽聽聽twodimshow(c,extent=(0,4,0,4))

聽聽聽聽聽聽title('That is what I want')

聽聽聽聽聽聽xlabel('X-Axis'); ylabel('Y-Axis')

聽聽聽聽聽聽show()

聽聽聽聽聽聽聽聽聽____________________________________________________________________________________

Never miss a thing. Make Yahoo your home page.

http://www.yahoo.com/r/hs

-------------------------------------------------------------------------

This SF.net email is sponsored by: Microsoft

Defy all challenges. Microsoft(R) Visual Studio 2008.

http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

聽聽聽聽聽聽____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs