contourf doesn't like a rectangular grid

Hi all,
I have been using contourf quite happily with a square number of grid points. e.g. a 20 by 20 grid. I recently decided to do a contourf plot of a 20 by 15 grid (300 points) and I get errors. Unfortunately, I am plotting experimental data so I cannot really tailor the grid size. Here is the error message ipython generates:
pylab.contourf((map_XX,map_YY,y))

···

TypeError Traceback (most recent call last)

/media/ANAMIKA/crashing out Raman/script_7-03-10_co1_G-mode_spatial_map.py in ()
----> 1
2
3
4
5

/usr/lib/pymodules/python2.6/matplotlib/pyplot.pyc in contourf(*args, **kwargs)
1874 ax.hold(hold)
1875 try:
-> 1876 ret = ax.contourf(*args, **kwargs)
1877 draw_if_interactive()
1878 finally:

/usr/lib/pymodules/python2.6/matplotlib/axes.pyc in contourf(self, *args, **kwargs)
6816 if not self._hold: self.cla()
6817 kwargs[‘filled’] = True
-> 6818 return mcontour.ContourSet(self, *args, **kwargs)
6819 contourf.doc = mcontour.ContourSet.contour_doc
6820

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in init(self, ax, *args, **kwargs)
572 raise ValueError(‘Either colors or cmap must be None’)
573 if self.origin == ‘image’: self.origin = mpl.rcParams[‘image.origin’]
–> 574 x, y, z = self._contour_args(*args) # also sets self.levels,
575 # self.layers

576         if self.colors is not None:

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in _contour_args(self, *args)
759 if Nargs <= 2:
760 z = ma.asarray(args[0], dtype=np.float64)
–> 761 x, y = self._initialize_x_y(z)
762 elif Nargs <=4:
763 x,y,z = self._check_xyz(args[:3])

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in _initialize_x_y(self, z)
696 ‘’’
697 if z.ndim != 2:
–> 698 raise TypeError(“Input must be a 2D array.”)
699 else:
700 Ny, Nx = z.shape

TypeError: Input must be a 2D array.

If anyone has a solution, please do let me know!
Many thanks!

Shrividya Ravi wrote:

Hi all,
I have been using contourf quite happily with a square number of grid points. e.g. a 20 by 20 grid. I recently decided to do a contourf plot of a 20 by 15 grid (300 points) and I get errors. Unfortunately, I am plotting experimental data so I cannot really tailor the grid size. Here is the error message ipython generates:

*pylab.contourf((map_XX,map_YY,y))

It looks like the problem is that you have an extra set of parentheses, so that you are giving contourf a single argument--a tuple--instead of the three arguments that you intend. Contour and contourf have no problem with rectangular grids.

Eric

···

*
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/media/ANAMIKA/crashing out Raman/script_7-03-10_co1_G-mode_spatial_map.py in <module>()
----> 1
      2
      3
      4
      5

/usr/lib/pymodules/python2.6/matplotlib/pyplot.pyc in contourf(*args, **kwargs)
   1874 ax.hold(hold)
   1875 try:
-> 1876 ret = ax.contourf(*args, **kwargs)
   1877 draw_if_interactive()
   1878 finally:

/usr/lib/pymodules/python2.6/matplotlib/axes.pyc in contourf(self, *args, **kwargs)
   6816 if not self._hold: self.cla()
   6817 kwargs['filled'] = True
-> 6818 return mcontour.ContourSet(self, *args, **kwargs)
   6819 contourf.__doc__ = mcontour.ContourSet.contour_doc
   6820

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in __init__(self, ax, *args, **kwargs)
    572 raise ValueError('Either colors or cmap must be None')
    573 if self.origin == 'image': self.origin = mpl.rcParams['image.origin']
--> 574 x, y, z = self._contour_args(*args) # also sets self.levels,
    575 # self.layers

    576 if self.colors is not None:

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in _contour_args(self, *args)
    759 if Nargs <= 2:
    760 z = ma.asarray(args[0], dtype=np.float64)
--> 761 x, y = self._initialize_x_y(z)
    762 elif Nargs <=4:
    763 x,y,z = self._check_xyz(args[:3])

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in _initialize_x_y(self, z)
    696 '''
    697 if z.ndim != 2:
--> 698 raise TypeError("Input must be a 2D array.")
    699 else:
    700 Ny, Nx = z.shape

TypeError: Input must be a 2D array.

If anyone has a solution, please do let me know!
Many thanks!

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

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

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

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hi there,
I think I made a mistake with putting in a tuple in my first email, but here is the error output from the contourf that I call within a script. I use this script before with a square grid and there is no problem. Anyway, after your email, I played with the script again and I think I have a problem with reshaping the ‘z’ data. For some reason, the ‘z’ data is not shaped the same as the meshgrid. I will try and sort it out…

Many thanks for all your help!
Shrividya

TypeError Traceback (most recent call last)

/media/ANAMIKA/crashing out Raman/script_7-03-10_co1_G-mode_spatial_map.py in ()
31
32 ## This part plots the contour plot and configures all the visual aspects.

—> 33 pylab.contourf(map_XX, map_YY, d)
34 pylab.colorbar()
35 pylab.xlim(min(map_x), max(map_x))

/usr/lib/pymodules/python2.6/matplotlib/pyplot.pyc in contourf(*args, **kwargs)
1874 ax.hold(hold)
1875 try:
-> 1876 ret = ax.contourf(*args, **kwargs)
1877 draw_if_interactive()
1878 finally:

/usr/lib/pymodules/python2.6/matplotlib/axes.pyc in contourf(self, *args, **kwargs)
6816 if not self._hold: self.cla()
6817 kwargs[‘filled’] = True
-> 6818 return mcontour.ContourSet(self, *args, **kwargs)
6819 contourf.doc = mcontour.ContourSet.contour_doc
6820

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in init(self, ax, *args, **kwargs)
572 raise ValueError(‘Either colors or cmap must be None’)
573 if self.origin == ‘image’: self.origin = mpl.rcParams[‘image.origin’]
–> 574 x, y, z = self._contour_args(*args) # also sets self.levels,
575 # self.layers

576         if self.colors is not None:

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in _contour_args(self, *args)
761 x, y = self._initialize_x_y(z)
762 elif Nargs <=4:
–> 763 x,y,z = self._check_xyz(args[:3])
764 else:
765 raise TypeError(“Too many arguments to %s; see help(%s)” % (fn,fn))

/usr/lib/pymodules/python2.6/matplotlib/contour.pyc in _check_xyz(self, args)
742 return x,y,z
743 if x.ndim != 1 or y.ndim != 1:
–> 744 raise TypeError(“Inputs x and y must be 1D or 2D.”)
745 nx, = x.shape
746 ny, = y.shape

TypeError: Inputs x and y must be 1D or 2D.
WARNING: Failure executing file: <script_7-03-10_co1_G-mode_spatial_map.py>

I deem it useful if you would add a

print map_XX.shape, map_YY.shape, y.shape .

I'm suspicious about their shape. _check_xyz() accepts 2D X,Y-arrays
only if their shape is equal to that of y (y in your case).

Friedrich