FW: basemap.contourf, colormap, extend='none', & levels array question

Im experiencing unexpected behavior with contourf.
Im trying to plot float values of '0.0', while also using the following levels array with contourf():

Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)

I've attached a sample image of the output.
the areas in white are contours where the plot values are exactly '0.0'.

I am not using the "extend='min|both'" in my contourf() function call.
If I use the "extend='min|both'" option, it will plot the '0.0' plot values using the color of the 1st-2nd level (blue). However, for my application, I DO NOT want to use the 'extend' option; I want my contour & colorbar levels to start at 0.

So, in theory,the '0.0' float values *should* be considered to be at the '0->1' range within my levels array, correct?

Im not sure how to create an example that can reproduce my data with '0.0' float values.
this is more/less the basis of the code I used to create the attached plot image:

# assume Z is my dataset

Z=create_my_data()

# setup basemap instance
# I call contourf from the basemap instance
# but the problem still exists even when
# contourf is called from pyplot instance

m=basemap(...)
...

print "Z: ",Z

# this prints the following
# this is the best I can do to demonstrate what my data in 'Z' looks like

wav-1.png

···

#
# Z: [[11.9459991455 11.9789991379 12.0119991302 ..., 8.51399993896
# 8.05200004578 7.55699968338]
# [12.375 12.4079999924 12.4409999847 ..., 9.00899982452 8.57999992371
# 8.08500003815]
# [12.8039999008 12.8369998932 12.8699998856 ..., 9.43799972534
# 9.07499980927 8.64599990845]
# ...,
# [0.0 0.0 0.0 ..., 4.52099990845 5.2469997406 5.90700006485]
# [0.0 0.0 0.0 ..., 6.13800001144 6.33599996567 6.73199987411]
# [0.0 0.0 0.0 ..., 4.98299980164 5.64299964905 6.26999998093]]

# my plotting code

Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)
norm = mpl.colors.BoundaryNorm(Lv,256)
cs = m.contourf(X,Y,Z,Lv,norm=norm,cmap=cm.jet)
plt.colorbar(cs)
plt.show()

I believe that matplotlib is having issues with comparing the "zero"- value level & '0.0' array values in my plot; it seems like its considering the '0.0' values to be 'less than zero' and thus not plotting them within the 'zero-through-one' level of my levels array. It seems as though its plotting these '0.0' values using the color assigned to the cmap's "set_under" property (anything below the 1st layer gets plotted in white or the 'set_under' color). is this correct?

Ive tried setting the first value in my levels array to '0.0' instead of just '0', but the results were the same.

Is this a bug; the fact that the '0.0' are not being associated with the '0' level?

Please help,
P.Romero
_________________________________________________________________
Windows Live™: Life without walls.
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_allup_1a_explore_032009

Pablo Romero wrote:

Im experiencing unexpected behavior with contourf.
Im trying to plot float values of '0.0', while also using the following levels array with contourf():

Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)

I've attached a sample image of the output.
the areas in white are contours where the plot values are exactly '0.0'.

The land areas, right?

In a simple test, I don't see what you are describing:

In [1]:Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)

In [2]:norm = mpl.colors.BoundaryNorm(Lv,256)

In [4]:norm([0.0])
Out[4]:
masked_array(data = [0],
              mask = [False],
        fill_value = 999999)

In [5]:z = linspace(0.0, 19.0, 20).reshape(4,5)

In [6]:z
Out[6]:
array([[ 0., 1., 2., 3., 4.],
        [ 5., 6., 7., 8., 9.],
        [ 10., 11., 12., 13., 14.],
        [ 15., 16., 17., 18., 19.]])

In [7]:contourf(z, norm=norm)
Out[7]:<matplotlib.contour.ContourSet instance at 0x9eaadac>

In [8]:contourf(z, Lv, norm=norm)
Out[8]:<matplotlib.contour.ContourSet instance at 0xa1ca98c>

0.0 is getting mapped to 0 by the norm, and that is used as an index to select the first cmap color, the darkest blue.

Is your Z an ndarray or is it a masked array? What you describe is consistent with the 0 values actually being masked, so they are not being contoured at all--except that contourf has a bug with internal masked regions so that they don't always get left unfilled.

In the example above, compare with

In [15]:figure()
Out[15]:<matplotlib.figure.Figure object at 0xa3879cc>

In [16]:contourf(ma.masked_less_equal(z, 0.0), Lv, norm=norm)
Out[16]:<matplotlib.contour.ContourSet instance at 0xa38790c>

Now, the problem with this hypothesis is that printing the masked array does *not* show the 0.0 value:

In [14]:print ma.masked_less_equal(z, 0.0)
[[-- 1.0 2.0 3.0 4.0]
  [5.0 6.0 7.0 8.0 9.0]
  [10.0 11.0 12.0 13.0 14.0]
  [15.0 16.0 17.0 18.0 19.0]]

so I don't understand what is going on in your case.

What is the result from typing

type(Z)
norm([Z[-1,0]])

in ipython (after generating Z, of course)?

I am not using the "extend='min|both'" in my contourf() function call.
If I use the "extend='min|both'" option, it will plot the '0.0' plot values using the color of the 1st-2nd level (blue). However, for my application, I DO NOT want to use the 'extend' option; I want my contour & colorbar levels to start at 0.

We should figure out what the problem really is. If the 0.0 (land) values are being treated as below the 0 boundary, then you should be able to control their color like this:

cmap = cm.jet
cmap.set_under(cmap(0)) # to make it the same as the 0-1 color
contourf(X, Y, Z, Lv, norm=norm, cmap=cmap)

So, in theory,the '0.0' float values *should* be considered to be at the '0->1' range within my levels array, correct?

Yes, and as noted above, when I test it, it is.

Eric

···

Im not sure how to create an example that can reproduce my data with '0.0' float values.
this is more/less the basis of the code I used to create the attached plot image:

# assume Z is my dataset

Z=create_my_data()

# setup basemap instance
# I call contourf from the basemap instance
# but the problem still exists even when
# contourf is called from pyplot instance

m=basemap(...)
...

print "Z: ",Z

# this prints the following
# this is the best I can do to demonstrate what my data in 'Z' looks like
#
# Z: [[11.9459991455 11.9789991379 12.0119991302 ..., 8.51399993896
# 8.05200004578 7.55699968338]
# [12.375 12.4079999924 12.4409999847 ..., 9.00899982452 8.57999992371
# 8.08500003815]
# [12.8039999008 12.8369998932 12.8699998856 ..., 9.43799972534
# 9.07499980927 8.64599990845]
# ...,
# [0.0 0.0 0.0 ..., 4.52099990845 5.2469997406 5.90700006485]
# [0.0 0.0 0.0 ..., 6.13800001144 6.33599996567 6.73199987411]
# [0.0 0.0 0.0 ..., 4.98299980164 5.64299964905 6.26999998093]]

# my plotting code

Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)
norm = mpl.colors.BoundaryNorm(Lv,256)
cs = m.contourf(X,Y,Z,Lv,norm=norm,cmap=cm.jet)
plt.colorbar(cs)
plt.show()

I believe that matplotlib is having issues with comparing the "zero"- value level & '0.0' array values in my plot; it seems like its considering the '0.0' values to be 'less than zero' and thus not plotting them within the 'zero-through-one' level of my levels array. It seems as though its plotting these '0.0' values using the color assigned to the cmap's "set_under" property (anything below the 1st layer gets plotted in white or the 'set_under' color). is this correct?

Ive tried setting the first value in my levels array to '0.0' instead of just '0', but the results were the same.

Is this a bug; the fact that the '0.0' are not being associated with the '0' level?

Please help,
P.Romero

What is the result from typing

type(Z)
norm([Z[-1,0]])

Actually, I don't know how your data are arranged, so try all 4 corners:

norm([Z[0,0], Z[0,-1], Z[-1,0], Z[-1,-1]])

Eric

it appears to be a masked array, but a "customized" one, that's being created from the interface to the external program Im working with (application named GrADS). here's some more info...

[7] ga-> Z?
Type: GaField
Base Class:
String Form:
[[0.0 0.0 0.0 ..., 0.0 0.0 0.0]
            [0.0 0.0 0.0 ..., 0.0 0.0 0.0]
            [0.0 0.0 0.0 ..., 0.0 0.0 0.0]
             1.97999989986 ..., 0.924000024796
            1.41899991035 1.58399999142]
            [0.0 0.0 0.0 ..., 0.0 0.0 0.0]]
Namespace: Interactive
Length: 311
File: /usr/lib/python2.5/site-packages/grads/numtypes.py
Docstring:
    This is GraDS version of a n-dimensional array: a masked array with
    a *grid* containing coordinate/dimension information attached to it.
    As a MaskedArray, GaField objects may possibly have masked values.
    Masked values of 1 exclude the corresponding element from any
    computation.
    Construction:
        x = GaField (data, name=None, Grid=None,
                     dtype=None, copy=True, order=False,
                     mask = nomask, fill_value=None)
    If copy=False, every effort is made not to copy the data:
        If data is a MaskedArray, and argument mask=nomask,
        then the candidate data is data.data and the
        mask used is data.mask. If data is a numeric array,
        it is used as the candidate raw data.
        If dtype is not None and
        is != data.dtype.char then a data copy is required.
        Otherwise, the candidate is used.
    If a data copy is required, raw data stored is the result of:
    numeric.array(data, dtype=dtype.char, copy=copy)
    If mask is nomask there are no masked values. Otherwise mask must
    be convertible to an array of booleans with the same shape as x.
    fill_value is used to fill in masked values when necessary,
    such as when printing and in method/function filled().
    The fill_value is not used for computation within this module.

[8] ga-> Z.mask
  Out[8]:
array([[False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       ...,
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]], dtype=bool)

[12] ga-> np.linalg.norm([Z[0,0], Z[0,-1], Z[-1,0], Z[-1,-1]])
  Out[12]: 0.0

what other info should I provide?

Pablo

···

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

Date: Sun, 15 Mar 2009 17:38:32 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

What is the result from typing

type(Z)
norm([Z[-1,0]])

Actually, I don't know how your data are arranged, so try all 4 corners:

norm([Z[0,0], Z[0,-1], Z[-1,0], Z[-1,-1]])

Eric

_________________________________________________________________
Windows Live™: Life without walls.
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_allup_1a_explore_032009

Pablo Romero wrote:

it appears to be a masked array, but a "customized" one, that's being created from the interface to the external program Im working with (application named GrADS). here's some more info...

Aha, very interesting! I know about grads but don't use it, and I did not know about the python interface.

Out of curiosity, why are you using the grads data interface but not the grads plotting?

[7] ga-> Z?
Type: GaField
Base Class: String Form:
[[0.0 0.0 0.0 ..., 0.0 0.0 0.0]
            [0.0 0.0 0.0 ..., 0.0 0.0 0.0]
             1.97999989986 ..., 0.924000024796
            1.41899991035 1.58399999142]
            [0.0 0.0 0.0 ..., 0.0 0.0 0.0]]
Namespace: Interactive
Length: 311
File: /usr/lib/python2.5/site-packages/grads/numtypes.py
Docstring:
    This is GraDS version of a n-dimensional array: a masked array with
    a *grid* containing coordinate/dimension information attached to it.
    As a MaskedArray, GaField objects may possibly have masked values.
    Masked values of 1 exclude the corresponding element from any
    computation.
    Construction:
        x = GaField (data, name=None, Grid=None,
                     dtype=None, copy=True, order=False,
                     mask = nomask, fill_value=None)
    If copy=False, every effort is made not to copy the data:
        If data is a MaskedArray, and argument mask=nomask,
        then the candidate data is data.data and the
        mask used is data.mask. If data is a numeric array,
        it is used as the candidate raw data.
        If dtype is not None and
        is != data.dtype.char then a data copy is required.
        Otherwise, the candidate is used.
    If a data copy is required, raw data stored is the result of:
    numeric.array(data, dtype=dtype.char, copy=copy)
    If mask is nomask there are no masked values. Otherwise mask must
    be convertible to an array of booleans with the same shape as x.
    fill_value is used to fill in masked values when necessary,
    such as when printing and in method/function filled().
    The fill_value is not used for computation within this module.

  [8] ga-> Z.mask
  Out[8]:
array([[False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       ...,
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]], dtype=bool)

  Out[12]: 0.0

What I meant was the BoundaryNorm instance "norm" that you are using to contour. But that's OK; we don't need that diagnostic now.

The mystery is solved; the 0 values in your Z array are not really 0, they are masked, and contourf is handling them correctly. 0.0 is just the fill value. If you want that to be the *real* value, then instead of using Z as the argument to your contourf call, use Z.filled().

Eric

Eric,

the GrADS interface is called 'pygrads'.
it was developed by arlindo da silva of the 'opengrads' project; a project aimed at extending & enhancing the capabilities of grads by adding 'extensions' to the core grads engine.

Many users are switching to plotting with pygrads due to the fact that matplotlib is a much richer, more powerful, more mature/extensive plotting environment.

So, GrADS users can manipulate gridded data in grads, export through pygrads, and create nicer/more elaborate plots using matplotlib :wink:

http://opengrads.org/wiki/index.php?title=Python_Interface_to_GrADS

as for the solution you offered, I tried doing the following:

cs = m.contourf(X,Y,Z.filled(),Lv,norm=norm,cmap=plt.cm.jet)

but I still got a plot with the '0.0' (land) areas plotted as 'white'/missing?.

Does it matter that the output I provided from the "Z.mask" command showed all 'false' for the mask array?

Is there any other info about the 'Z' grads-masked array that I can provide that might help figure out what is going wrong?

Pablo

···

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

Date: Sun, 15 Mar 2009 18:01:08 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

it appears to be a masked array, but a "customized" one, that's being created from the interface to the external program Im working with (application named GrADS). here's some more info...

Aha, very interesting! I know about grads but don't use it, and I did
not know about the python interface.

Out of curiosity, why are you using the grads data interface but not the
grads plotting?

[7] ga-> Z?
Type: GaField
Base Class:
String Form:
[[0.0 0.0 0.0 ..., 0.0 0.0 0.0]
[0.0 0.0 0.0 ..., 0.0 0.0 0.0]
[0.0 0.0 0.0 ..., 0.0 0.0 0.0]
1.97999989986 ..., 0.924000024796
1.41899991035 1.58399999142]
[0.0 0.0 0.0 ..., 0.0 0.0 0.0]]
Namespace: Interactive
Length: 311
File: /usr/lib/python2.5/site-packages/grads/numtypes.py
Docstring:
This is GraDS version of a n-dimensional array: a masked array with
a *grid* containing coordinate/dimension information attached to it.
As a MaskedArray, GaField objects may possibly have masked values.
Masked values of 1 exclude the corresponding element from any
computation.
Construction:
x = GaField (data, name=None, Grid=None,
dtype=None, copy=True, order=False,
mask = nomask, fill_value=None)
If copy=False, every effort is made not to copy the data:
If data is a MaskedArray, and argument mask=nomask,
then the candidate data is data.data and the
mask used is data.mask. If data is a numeric array,
it is used as the candidate raw data.
If dtype is not None and
is != data.dtype.char then a data copy is required.
Otherwise, the candidate is used.
If a data copy is required, raw data stored is the result of:
numeric.array(data, dtype=dtype.char, copy=copy)
If mask is nomask there are no masked values. Otherwise mask must
be convertible to an array of booleans with the same shape as x.
fill_value is used to fill in masked values when necessary,
such as when printing and in method/function filled().
The fill_value is not used for computation within this module.

[8] ga-> Z.mask
Out[8]:
array([[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]], dtype=bool)

[12] ga-> np.linalg.norm([Z[0,0], Z[0,-1], Z[-1,0], Z[-1,-1]])
Out[12]: 0.0

What I meant was the BoundaryNorm instance "norm" that you are using to
contour. But that's OK; we don't need that diagnostic now.

The mystery is solved; the 0 values in your Z array are not really 0,
they are masked, and contourf is handling them correctly. 0.0 is just
the fill value. If you want that to be the *real* value, then instead of
using Z as the argument to your contourf call, use Z.filled().

Eric

_________________________________________________________________
Windows Live™ Groups: Create an online spot for your favorite groups to meet.
http://windowslive.com/online/groups?ocid=TXT_TAGLM_WL_groups_032009

Pablo Romero wrote:

Eric,
the GrADS interface is called 'pygrads'. it was developed by arlindo da silva of the 'opengrads' project; a project aimed at extending & enhancing the capabilities of grads by adding 'extensions' to the core grads engine.
Many users are switching to plotting with pygrads due to the fact that matplotlib is a much richer, more powerful, more mature/extensive plotting environment.
So, GrADS users can manipulate gridded data in grads, export through pygrads, and create nicer/more elaborate plots using matplotlib :wink:
http://opengrads.org/wiki/index.php?title=Python_Interface_to_GrADS

cs = m.contourf(X,Y,Z.filled(),Lv,norm=norm,cmap=plt.cm.jet)
but I still got a plot with the '0.0' (land) areas plotted as 'white'/missing?.
Does it matter that the output I provided from the "Z.mask" command showed all 'false' for the mask array?

All this is strange again. I did not expect Z.mask to be all False.

Try

import numpy as np
Znp = np.array(Z.filled(0.0))

and then contour that instead of Z. If that doesn't work, then check the corner values of Znp:

Znp[0,0], Znp[-1,0] etc.

Eric

Eric,
Its Still not working, still getting white/empty areas where Z=0.0
Here's my ipython output:

[6] ga-> Znp=np.array(Z.filled())
[7] ga-> Znp
  Out[7]:
array([[ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ],
       [ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ],
       [ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ],
       ...,
       [ 1.023 , 1.551 , 2.5079999 , ..., 0.72600001,
         0.85799998, 1.023 ],
       [ 1.58399999, 1.74899995, 1.9799999 , ..., 0.92400002,
         1.41899991, 1.58399999],
       [ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ]])
[8] ga-> Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)
[9] ga-> norm = mpl.colors.BoundaryNorm(Lv,256)
[10] plt.contourf(Znp, Lv, norm=norm)
  Out[10]:
[11] Znp[0,0]
  Out[11]: 0.0
[12] Znp[-1,0]
  Out[12]: 0.0
[13] Znp[1,0]
  Out[13]: 0.0
[14] Znp[0,1]
  Out[14]: 0.0
[15] Znp[0,-1]
  Out[15]: 0.0

This is very frustrating. :frowning:

Is there anything else Im missing here?
anything else I can try?

Im beginning to think that negative/"less than zero" values are actually being plotted in those land areas, even though my array is only showing zeros, '0.0'.

Is there any way I can plot the output in a grid format?
i.e. print the text value of each point in the array at each point on the plot?
This way, I can see if a '0.0' value is really being plotted in those white/blank areas.

Or, is there another/better way to keep diagnosing the problem?

P.Romero

···

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

Date: Sun, 15 Mar 2009 19:38:36 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

Eric,

the GrADS interface is called 'pygrads'.
it was developed by arlindo da silva of the 'opengrads' project; a project aimed at extending & enhancing the capabilities of grads by adding 'extensions' to the core grads engine.

Many users are switching to plotting with pygrads due to the fact that matplotlib is a much richer, more powerful, more mature/extensive plotting environment.

So, GrADS users can manipulate gridded data in grads, export through pygrads, and create nicer/more elaborate plots using matplotlib :wink:

http://opengrads.org/wiki/index.php?title=Python_Interface_to_GrADS

as for the solution you offered, I tried doing the following:

cs = m.contourf(X,Y,Z.filled(),Lv,norm=norm,cmap=plt.cm.jet)

but I still got a plot with the '0.0' (land) areas plotted as 'white'/missing?.

Does it matter that the output I provided from the "Z.mask" command showed all 'false' for the mask array?

All this is strange again. I did not expect Z.mask to be all False.

Try

import numpy as np
Znp = np.array(Z.filled(0.0))

and then contour that instead of Z. If that doesn't work, then check
the corner values of Znp:

Znp[0,0], Znp[-1,0] etc.

Eric

_________________________________________________________________
Windows Live™ Contacts: Organize your contact list.
http://windowslive.com/connect/post/marcusatmicrosoft.spaces.live.com-Blog-cns!503D1D86EBB2B53C!2285.entry?ocid=TXT_TAGLM_WL_UGC_Contacts_032009

Pablo Romero wrote:

Eric,
Its Still not working, still getting white/empty areas where Z=0.0
Here's my ipython output:
  [6] ga-> Znp=np.array(Z.filled())
[7] ga-> Znp
  Out[7]:
array([[ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ],
       [ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ],
       [ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ],
       ...,
       [ 1.023 , 1.551 , 2.5079999 , ..., 0.72600001,
         0.85799998, 1.023 ],
       [ 1.58399999, 1.74899995, 1.9799999 , ..., 0.92400002,
         1.41899991, 1.58399999],
       [ 0. , 0. , 0. , ..., 0. ,
         0. , 0. ]])
[8] ga-> Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)
[9] ga-> norm = mpl.colors.BoundaryNorm(Lv,256)
[10] plt.contourf(Znp, Lv, norm=norm)
  Out[10]: [11] Znp[0,0]
  Out[11]: 0.0
[12] Znp[-1,0]
  Out[12]: 0.0
[13] Znp[1,0]
  Out[13]: 0.0
[14] Znp[0,1]
  Out[14]: 0.0
[15] Znp[0,-1]
  Out[15]: 0.0

You missed the [-1,-1] corner. I thought at least one corner would be non-zero, but maybe not. At this point, it doesn't matter.

This is very frustrating. :frowning:
Is there anything else Im missing here?
anything else I can try?

Now I am wondering whether there is any difference between your numpy/matplotlib installation and mine that is making this difference--although that really does not make any sense, either. But let's check it. Please use the same method as above to make Xnp and Ynp, and then use np.savez to write out a file with Xnp, Ynp, and Znp:

np.savez('XYZ.npz', Xnp=Xnp, Ynp=Ynp, Znp=Znp)

and ftp XYZ.npz to ftp://currents.soest.hawaii.edu/pub/incoming. Then I can easily try to reproduce exactly what you are doing, and we will see if I get the same result, or a different one.

Im beginning to think that negative/"less than zero" values are actually being plotted in those land areas, even though my array is only showing zeros, '0.0'.

I don't see how this could be.

Is there any way I can plot the output in a grid format?
i.e. print the text value of each point in the array at each point on the plot?
This way, I can see if a '0.0' value is really being plotted in those white/blank areas.

Not trivially; among other things, I think you have too many points. And you have already verified that the values print as 0.0. I am sure they really are 0.0.

Or, is there another/better way to keep diagnosing the problem?

I can't think of anything now other than letting me try it on my system, as suggested above.

You might also see what happens with a workaround:

Znp0 = Znp.copy()
Znp0[Znp==0] = 0.1

Then try plotting Znp0.

Eric

Eric,
I've uploaded the file.
The only thing I should mention is that my 'X' and 'Y' arrays are not masked arrays, they are 2d arrays that I created using the meshgrid function.

I ended up doing this

Xnp=np.array(X)
Ynp=np.array(Y)

which probably wasnt necessary..

Let me know if the file worked for you.

Pablo

···

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

Date: Sun, 15 Mar 2009 21:14:01 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

Eric,
Its Still not working, still getting white/empty areas where Z=0.0
Here's my ipython output:

[6] ga-> Znp=np.array(Z.filled())
[7] ga-> Znp
Out[7]:
array([[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ],
[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ],
[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ],
...,
[ 1.023 , 1.551 , 2.5079999 , ..., 0.72600001,
0.85799998, 1.023 ],
[ 1.58399999, 1.74899995, 1.9799999 , ..., 0.92400002,
1.41899991, 1.58399999],
[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ]])
[8] ga-> Lv=(0,1,3,5,6,7,8,9,10,12,14,16,18,20,25,30,35,40,50,75)
[9] ga-> norm = mpl.colors.BoundaryNorm(Lv,256)
[10] plt.contourf(Znp, Lv, norm=norm)
Out[10]:
[11] Znp[0,0]
Out[11]: 0.0
[12] Znp[-1,0]
Out[12]: 0.0
[13] Znp[1,0]
Out[13]: 0.0
[14] Znp[0,1]
Out[14]: 0.0
[15] Znp[0,-1]
Out[15]: 0.0

You missed the [-1,-1] corner. I thought at least one corner would be
non-zero, but maybe not. At this point, it doesn't matter.

This is very frustrating. :frowning:

Is there anything else Im missing here?
anything else I can try?

Now I am wondering whether there is any difference between your
numpy/matplotlib installation and mine that is making this
difference--although that really does not make any sense, either. But
let's check it. Please use the same method as above to make Xnp and
Ynp, and then use np.savez to write out a file with Xnp, Ynp, and Znp:

np.savez('XYZ.npz', Xnp=Xnp, Ynp=Ynp, Znp=Znp)

and ftp XYZ.npz to ftp://currents.soest.hawaii.edu/pub/incoming. Then I
can easily try to reproduce exactly what you are doing, and we will see
if I get the same result, or a different one.

Im beginning to think that negative/"less than zero" values are actually being plotted in those land areas, even though my array is only showing zeros, '0.0'.

I don't see how this could be.

Is there any way I can plot the output in a grid format?
i.e. print the text value of each point in the array at each point on the plot?
This way, I can see if a '0.0' value is really being plotted in those white/blank areas.

Not trivially; among other things, I think you have too many points. And
you have already verified that the values print as 0.0. I am sure they
really are 0.0.

Or, is there another/better way to keep diagnosing the problem?

I can't think of anything now other than letting me try it on my system,
as suggested above.

You might also see what happens with a workaround:

Znp0 = Znp.copy()
Znp0[Znp==0] = 0.1

Then try plotting Znp0.

Eric

_________________________________________________________________
Windows Live™ Groups: Create an online spot for your favorite groups to meet.
http://windowslive.com/online/groups?ocid=TXT_TAGLM_WL_groups_032009

Pablo Romero wrote:

Eric,
I've uploaded the file.
The only thing I should mention is that my 'X' and 'Y' arrays are not masked arrays, they are 2d arrays that I created using the meshgrid function.
I ended up doing this
Xnp=np.array(X)
Ynp=np.array(Y)
which probably wasnt necessary..
Let me know if the file worked for you.

I got the file and I see the problem. It may take a while for me to find the bug.

Eric

Eric,
For your reference, Im sending you a link to the python-grads interface files.

http://downloads.sourceforge.net/opengrads/pygrads-1.1.0.tar.gz

It wont function unless you have grads available, but perhaps it might help to take a look at how the python code is creating the GrADS masked arrays?

Pablo

···

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

Date: Mon, 16 Mar 2009 07:25:10 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

Eric,
I've uploaded the file.
The only thing I should mention is that my 'X' and 'Y' arrays are not masked arrays, they are 2d arrays that I created using the meshgrid function.

I ended up doing this

Xnp=np.array(X)
Ynp=np.array(Y)

which probably wasnt necessary..

Let me know if the file worked for you.

I got the file and I see the problem. It may take a while for me to
find the bug.

Eric

_________________________________________________________________
Hotmail® is up to 70% faster. Now good news travels really fast.
http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_HM_70faster_032009

Pablo Romero wrote:

Eric,
For your reference, Im sending you a link to the python-grads interface files.

Thank you.

I am coming to the conclusion that you were mostly right early on. I suspect that whether points landing on a contour boundary end up treated as above or below is not uniquely defined by the algorithm, but depends on the context. In my extremely simple examples, zero-values seem to land in the above-zero bin, but with your array it is the opposite--at least for the obvious large land masses.

This brings us back to the workaround I suggested earlier; add a small number to your points at zero to boost them up into the first bin.

Something like this could be done within the contour code, with the option of allowing one to specify that on-the-boundary is above, or that it is below. I don't think I would want to do this data-fudging automatically--partly because it would incur a speed penalty, partly because it really is fudging--but maybe it should be added as an option. I'm not enthusiastic about it. I would prefer to simply add a warning to the documentation and leave the fudging to the user.

Eric

···

http://downloads.sourceforge.net/opengrads/pygrads-1.1.0.tar.gz
It wont function unless you have grads available, but perhaps it might help to take a look at how the python code is creating the GrADS masked arrays?
Pablo

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

Date: Mon, 16 Mar 2009 07:25:10 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

Eric,
I've uploaded the file.
The only thing I should mention is that my 'X' and 'Y' arrays are not masked arrays, they are 2d arrays that I created using the meshgrid function.

I ended up doing this

Xnp=np.array(X)
Ynp=np.array(Y)

which probably wasnt necessary..

Let me know if the file worked for you.

I got the file and I see the problem. It may take a while for me to
find the bug.

Eric

_________________________________________________________________
Hotmail® is up to 70% faster. Now good news travels really fast. http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_HM_70faster_032009

Eric,

thanks for all of the help.

Im going to do what you suggested; find a way to add a very small amount to my '0.0' values, as this should definitely place them in the 'above zero boundary' bin.

Thanks again,

P.Romero

···

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

Date: Mon, 16 Mar 2009 08:58:23 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

Eric,
For your reference, Im sending you a link to the python-grads interface files.

Thank you.

I am coming to the conclusion that you were mostly right early on. I
suspect that whether points landing on a contour boundary end up treated
as above or below is not uniquely defined by the algorithm, but depends
on the context. In my extremely simple examples, zero-values seem to
land in the above-zero bin, but with your array it is the opposite--at
least for the obvious large land masses.

This brings us back to the workaround I suggested earlier; add a small
number to your points at zero to boost them up into the first bin.

Something like this could be done within the contour code, with the
option of allowing one to specify that on-the-boundary is above, or that
it is below. I don't think I would want to do this data-fudging
automatically--partly because it would incur a speed penalty, partly
because it really is fudging--but maybe it should be added as an option.
I'm not enthusiastic about it. I would prefer to simply add a warning
to the documentation and leave the fudging to the user.

Eric

http://downloads.sourceforge.net/opengrads/pygrads-1.1.0.tar.gz

It wont function unless you have grads available, but perhaps it might help to take a look at how the python code is creating the GrADS masked arrays?

Pablo

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

Date: Mon, 16 Mar 2009 07:25:10 -1000
From: efiring@...202...
Subject: Re: [Matplotlib-users] FW: basemap.contourf, colormap, extend='none', & levels array question
To: romero619@...32...
CC: matplotlib-users@lists.sourceforge.net

Pablo Romero wrote:

Eric,
I've uploaded the file.
The only thing I should mention is that my 'X' and 'Y' arrays are not masked arrays, they are 2d arrays that I created using the meshgrid function.

I ended up doing this

Xnp=np.array(X)
Ynp=np.array(Y)

which probably wasnt necessary..

Let me know if the file worked for you.

I got the file and I see the problem. It may take a while for me to
find the bug.

Eric

_________________________________________________________________
Hotmail® is up to 70% faster. Now good news travels really fast.
http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_HM_70faster_032009

_________________________________________________________________
Windows Live™ Groups: Create an online spot for your favorite groups to meet.
http://windowslive.com/online/groups?ocid=TXT_TAGLM_WL_groups_032009

Pablo Romero wrote:

Eric,
thanks for all of the help.
Im going to do what you suggested; find a way to add a very small amount to my '0.0' values, as this should definitely place them in the 'above zero boundary' bin.

Z[Z==0] = 0.1

That's all you need, it appears.

Eric