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




----- Original Message ----

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


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

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


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 ( and/or (SLAC HIPPO (

------------------- 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])


if __name__ == "__main__":

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

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

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




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

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

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


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

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

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



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

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



Never miss a thing. Make Yahoo your home page.


This email is sponsored by: Microsoft

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


Matplotlib-users mailing list

Never miss a thing. Make Yahoo your home page.