imshow and pcolor differences

Hello,

I want to display some data as an image and also as a contour.

I have been looking at imshow and pcolor and find that contour
and imshow are behaving differently than pcolor. In the example below I
have a 5x5 image. pshow displays the pixels but imshow and contour shows
resampling artifacts since they resample offset by 0.5pixels.

The advantage of imshow is that the pixels are square which is what I
want. I also want to use contour which also seems to show the same
type of resampling as imshow.

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

image = np.random.rand(5,5)

plt.figure()
plt.pcolor(image)
plt.title('pcolor defaults')

plt.figure()
plt.imshow(image, origin='lower')
plt.title('imshow defaults with origin=lower')

plt.show()

Is there a method to force imshow to not resample the image
It is not obvius to me from reading the help for imshow and pcolor.

Thanks, richard

···

-------------------------------------------------------------------
Dr. Richard G. McMahon | Phone (office) 44-(0)-1223-337519
University of Cambridge | (switchboard) 1223-337548
Institute of Astronomy | (secretary) 1223-337516
Madingley Rd | FAX 1223-337523
Cambridge, CB3 OHA, UK. | mobile 7885-409019
Office: Hoyle 18 | home 1223-359770
-------------------------------------------------------------------
email: rgm@...240... | WWW: http://www.ast.cam.ac.uk/~rgm
richardgmcmahon@...287... | skype: richardgmcmahon
-------------------------------------------------------------------

import numpy as np

import matplotlib as mpl

import matplotlib.pyplot as plt

image = np.random.rand(5,5)

plt.figure()

plt.pcolor(image)

plt.title(‘pcolor defaults’)

plt.figure()

plt.imshow(image, origin=‘lower’)

plt.title(‘imshow defaults with origin=lower’)

plt.show()

Is there a method to force imshow to not resample the image

It is not obvius to me from reading the help for imshow and pcolor.

pass in interpolation=‘nearest’ as a keyword argument.

Ryan

···


Ryan May
Graduate Research Assistant

School of Meteorology
University of Oklahoma

Richard McMahon wrote:

Hello,

I want to display some data as an image and also as a contour.

I have been looking at imshow and pcolor and find that contour
and imshow are behaving differently than pcolor. In the example below I
have a 5x5 image. pshow displays the pixels but imshow and contour shows
resampling artifacts since they resample offset by 0.5pixels.

The advantage of imshow is that the pixels are square which is what I
want. I also want to use contour which also seems to show the same
type of resampling as imshow.

This is not a matter of resampling (unless I am misunderstanding you)--it is a difference in the grid. For imshow and contour, the data points and the grid intersections coincide; for pcolor (and pcolormesh), the grid gives the boundaries of colored quadrilaterals. Therefore, if the data array is MxN, then the the grid X dimension should be N+1 and the grid Y dimension should be M+1.

For an example of how to use contours with images, see:

http://matplotlib.sourceforge.net/examples/pylab_examples/contour_image.html

Also, it sounds like maybe you don't want imshow to interpolate: try using the interpolation='nearest' kwarg.

If you do want to use pcolor or pcolormesh or Axes.pcolorfast, then you can still get square pixels by suitable choice of aspect ratio. Try axis('equal') or axis('scaled'), or axis('image'), or use the set_aspect() method of the Axes instance.

Eric

···

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

image = np.random.rand(5,5)

plt.figure()
plt.pcolor(image)
plt.title('pcolor defaults')

plt.figure()
plt.imshow(image, origin='lower')
plt.title('imshow defaults with origin=lower')

plt.show()

Is there a method to force imshow to not resample the image
It is not obvius to me from reading the help for imshow and pcolor.

Thanks, richard

-------------------------------------------------------------------
Dr. Richard G. McMahon | Phone (office) 44-(0)-1223-337519
University of Cambridge | (switchboard) 1223-337548
Institute of Astronomy | (secretary) 1223-337516
Madingley Rd | FAX 1223-337523
Cambridge, CB3 OHA, UK. | mobile 7885-409019
Office: Hoyle 18 | home 1223-359770
-------------------------------------------------------------------
email: rgm@...240... | WWW: http://www.ast.cam.ac.uk/~rgm
richardgmcmahon@...287... | skype: richardgmcmahon
-------------------------------------------------------------------

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks Eric and also Ryan,

interpolation='nearest' worked. Maybe if the correct person is
listening, this could be highlighted in the imshow documentation since
the default of interpolation=none is not doing 'nothing' IMHO.

Also, I have looked

http://matplotlib.sourceforge.net/examples/pylab_examples/contour_image.html

This highlights the various issues nicely. I noticed that the contourf example has white space at the top.

Also, the use of arange(-3.0, 4.001, delta) implies some subtle
work around may be needed.

thanks

···

On Wed, 2 Sep 2009, Eric Firing wrote:

Date: Wed, 02 Sep 2009 15:08:49 -1000
From: Eric Firing <efiring@...202...>
To: Richard McMahon <rgm@...240...>
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] imshow and pcolor differences

Richard McMahon wrote:

Hello,

I want to display some data as an image and also as a contour.

I have been looking at imshow and pcolor and find that contour
and imshow are behaving differently than pcolor. In the example below I
have a 5x5 image. pshow displays the pixels but imshow and contour shows
resampling artifacts since they resample offset by 0.5pixels.

The advantage of imshow is that the pixels are square which is what I
want. I also want to use contour which also seems to show the same
type of resampling as imshow.

This is not a matter of resampling (unless I am misunderstanding you)--it is a difference in the grid. For imshow and contour, the data points and the grid intersections coincide; for pcolor (and pcolormesh), the grid gives the boundaries of colored quadrilaterals. Therefore, if the data array is MxN, then the the grid X dimension should be N+1 and the grid Y dimension should be M+1.

For an example of how to use contours with images, see:

http://matplotlib.sourceforge.net/examples/pylab_examples/contour_image.html

Also, it sounds like maybe you don't want imshow to interpolate: try using the interpolation='nearest' kwarg.

If you do want to use pcolor or pcolormesh or Axes.pcolorfast, then you can still get square pixels by suitable choice of aspect ratio. Try axis('equal') or axis('scaled'), or axis('image'), or use the set_aspect() method of the Axes instance.

Eric

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

image = np.random.rand(5,5)

plt.figure()
plt.pcolor(image)
plt.title('pcolor defaults')

plt.figure()
plt.imshow(image, origin='lower')
plt.title('imshow defaults with origin=lower')

plt.show()

Is there a method to force imshow to not resample the image
It is not obvius to me from reading the help for imshow and pcolor.

Thanks, richard

-------------------------------------------------------------------
  Dr. Richard G. McMahon | Phone (office) 44-(0)-1223-337519
  University of Cambridge | (switchboard) 1223-337548
  Institute of Astronomy | (secretary) 1223-337516
  Madingley Rd | FAX 1223-337523
  Cambridge, CB3 OHA, UK. | mobile 7885-409019
  Office: Hoyle 18 | home 1223-359770
-------------------------------------------------------------------
  email: rgm@...240... | WWW: http://www.ast.cam.ac.uk/~rgm
  richardgmcmahon@...287... | skype: richardgmcmahon
-------------------------------------------------------------------

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008
30-Day trial. Simplify your report design, integration and deployment -
and focus on what you do best, core application coding. Discover what's
new with Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
-------------------------------------------------------------------
  Dr. Richard G. McMahon | Phone (office) 44-(0)-1223-337519
  University of Cambridge | (switchboard) 1223-337548
  Institute of Astronomy | (secretary) 1223-337516
  Madingley Rd | FAX 1223-337523
  Cambridge, CB3 OHA, UK. | mobile 7885-409019
  Office: Hoyle 18 | home 1223-359770
-------------------------------------------------------------------
  email: rgm@...240... | WWW: http://www.ast.cam.ac.uk/~rgm
  richardgmcmahon@...287... | skype: richardgmcmahon
-------------------------------------------------------------------