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

[Matplotlib-users] imshow: axis not natural

 [Matplotlib-users] imshow: axis not natural From: Piotr Zolnierczuk - 2008-02-22 19:31:21 ```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 ```

 [Matplotlib-users] imshow: axis not natural From: Piotr Zolnierczuk - 2008-02-22 19:31:21 ```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 ```