problem with <type 'array'> in pcolor

The following code

···

======================
from matplotlib.matlab import *

x = arange(0,20,.2)
y = arange(0,20,.2)
X, Y = meshgrid(x,y)
z=zeros((len(x),len(y)),'f')
for i in enumerate(x):
    for j in enumerate(y):
        z[i[0]][j[0]]=10*sin(i[1]*j[1])
       #or z[i[0],j[0]]=10*sin(i[1]*j[1])
pcolor(X,Y, transpose(z),shading='faceted')
show()

breaks in the module color.py

=============================
   def get_color(self, val, valmin, valmax):
       # map val to a range
from 0 to 1
       if iterable(val):
          s = "val must be a scalar.
Perhaps you meant to call get_colors?"
          #print val,type(val)
          raise ValueError, s
       #print valmin, valmax
       #print
val,type(val)
       ind = self.indmax*(val-valmin)/(valmax-valmin)
       return
self.rgbs[self._bound_ind(ind)]

because the test for iterable fails since the element C[i,j] is type
<array>. One solution is to change the code section around line 1126 in
axes.py from c = C[i,j] to the following.

=====================
        for i in range(Nx-1):
            for j in range(Ny-1):

    c = C[i][j]

the form C[i][j] seems to always return float.

What you are seeing is one of the odd inconsistencies
present in Numeric regarding what kind of thing is returned
for a single element. This has been discussed on the numpy
list some years back.

a = zeros((3,3), 'f')
type(a[0,0])

<type 'array'>

type(a[0][0])

<type 'float'>

b = zeros((3,3), 'd')
type(b[0,0])

<type 'float'>

type(b[0][0])

<type 'float'>

So what kind of thing you get back when indexing a 2-d array
depends on both the type and dimensionality of the array.
The basic rule is that if the array is more than one dimension,
and not one of the basic python numerical types (e.g., 'f')
then indexing a single element tries to preserve the type by
returning a rank-0 array of the same type. Oddly though, indexing
a single element of a 1-d 'f' array returns a Python float scalar
(why the difference, I have no idea). This is why a[0][0] returns
something different than a[0,0] since one is indexing a 1-d array
(a[0]).

For numarray we decided that indexing a single element would always
return a Python scalar since that seemed to be what most expected.
There were those that argued that it should always return a rank-0
array, but we decided against that.

Perry

···

-----Original Message-----
From: matplotlib-devel-admin@lists.sourceforge.net
[mailto:matplotlib-devel-admin@lists.sourceforge.net]On Behalf Of rod
holland
Sent: Tuesday, May 11, 2004 4:59 PM
To: matplotlib-devel@lists.sourceforge.net
Subject: [matplotlib-devel] problem with <type 'array'> in pcolor

The following code

======================
from matplotlib.matlab import *

x = arange(0,20,.2)
y = arange(0,20,.2)
X, Y = meshgrid(x,y)
z=zeros((len(x),len(y)),'f')
for i in enumerate(x):
    for j in enumerate(y):
        z[i[0]][j[0]]=10*sin(i[1]*j[1])
       #or z[i[0],j[0]]=10*sin(i[1]*j[1])
pcolor(X,Y, transpose(z),shading='faceted')
show()

breaks in the module color.py

=============================
   def get_color(self, val, valmin, valmax):
       # map val to a range
from 0 to 1
       if iterable(val):
          s = "val must be a scalar.
Perhaps you meant to call get_colors?"
          #print val,type(val)
          raise ValueError, s
       #print valmin, valmax
       #print
val,type(val)
       ind = self.indmax*(val-valmin)/(valmax-valmin)
       return
self.rgbs[self._bound_ind(ind)]

because the test for iterable fails since the element C[i,j] is type
<array>. One solution is to change the code section around line 1126 in
axes.py from c = C[i,j] to the following.

=====================
        for i in range(Nx-1):
            for j in range(Ny-1):

    c = C[i][j]

the form C[i][j] seems to always return float.

-------------------------------------------------------
This SF.Net email is sponsored by Sleepycat Software
Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
deliver higher performing products faster, at low TCO.
http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel