Input array dimensions for contour or contourf

The following program seems to work with contour/contourf. However the documentation for the contourf function states

contour(X,Y,Z)

X, Y, and Z must be arrays with the same dimensions.”

I am finding that contour works if the dimension of X and Y are 1, but Z must be two-dimensional. The following program seems to bear this out. Are the arrays x and y below two-dimensional, or is the documentation misleading? Thanks for your help.

import numpy as N

import pylab as PLT

lons = N.linspace(-5.,5.,5) # Is this a one or two dimensional array?

lats = N.linspace(-3.,3.,4)

z = N.zeros((len(lats), len(lons)))

for i in range(len(lons)):

for j in range(len(lats)):

z[j,i]=i+j

PLT.clf()

PLT.contourf(lons,lats,z)

PLT.colorbar()

PLT.show()

-Tony

···

Tony Mannucci

Supervisor, Ionospheric and Atmospheric Remote Sensing Group

Mail-Stop 138-308, Tel > (818) 354-1699

Jet Propulsion Laboratory, Fax > (818) 393-5115

California Institute of Technology, Email > Tony.Mannucci@…369…

4800 Oak Grove Drive, http://genesis.jpl.nasa.gov

Pasadena, CA 91109

Tony,

contour and contourf seems to take advantage of numpy’s broadcasting feature, so it is probably more correct to say that X and Y must be at least broadcastable to the shape of Z. I think there are a number of functions where this may or may not be true, and at some point we (the developers) should agree on basic input array handling and make it consistent across all plotting functions.

So, technically speaking, the docs are “right”, but should be clearer in this case. I will add it to my doc-fixing commit that I will do today.

Ben Root

···

On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) <anthony.j.mannucci@…369…> wrote:

The following program seems to work with contour/contourf. However the documentation for the contourf function states

contour(X,Y,Z)

X, Y, and Z must be arrays with the same dimensions.”

I am finding that contour works if the dimension of X and Y are 1, but Z must be two-dimensional. The following program seems to bear this out. Are the arrays x and y below two-dimensional, or is the documentation misleading? Thanks for your help.

import numpy as N

import pylab as PLT

lons = N.linspace(-5.,5.,5) # Is this a one or two dimensional array?

lats = N.linspace(-3.,3.,4)

z = N.zeros((len(lats), len(lons)))

for i in range(len(lons)):

for j in range(len(lats)):

z[j,i]=i+j

PLT.clf()

PLT.contourf(lons,lats,z)

PLT.colorbar()

PLT.show()

-Tony

    The following program seems to work with contour/contourf. However
    the documentation for the contourf function states

    contour(X,Y,Z)

    "/X/, /Y/, and /Z/ must be arrays with the same dimensions."

    I am finding that contour works if the dimension of X and Y are 1,
    but Z must be two-dimensional. The following program seems to bear
    this out. Are the arrays x and y below two-dimensional, or is the
    documentation misleading? Thanks for your help.

    import numpy as N
    import pylab as PLT

    lons = N.linspace(-5.,5.,5) # Is this a one or two dimensional array?
    lats = N.linspace(-3.,3.,4)

    z = N.zeros((len(lats), len(lons)))
    for i in range(len(lons)):
         for j in range(len(lats)):
             z[j,i]=i+j

    PLT.clf()
    PLT.contourf(lons,lats,z)
    PLT.colorbar()
    PLT.show()

    -Tony

Tony,

contour and contourf seems to take advantage of numpy's broadcasting
feature, so it is probably more correct to say that X and Y must be at
least broadcastable to the shape of Z. I think there are a number of

Not quite; if x and y are 1-D, meshgrid is called to make 2-D versions, which must then match Z. Broadcasting is not used or supported. So, the contour docstring was not updated when this functionality was added, long ago. Consider it an undocumented feature, in need of documentation.

Eric

···

On 05/31/2011 05:50 AM, Benjamin Root wrote:

On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > <anthony.j.mannucci@…369… > <mailto:anthony.j.mannucci@…369…>> wrote:

functions where this may or may not be true, and at some point we (the
developers) should agree on basic input array handling and make it
consistent across all plotting functions.

So, technically speaking, the docs are "right", but should be clearer in
this case. I will add it to my doc-fixing commit that I will do today.

Ben Root

------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Data protection magic?
Nope - It's vRanger. Get your free trial download today.
http://p.sf.net/sfu/quest-sfdev2dev

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Well, (as a bit of a cop-out) in my edit, I didn’t say that they were broadcasted, only that they must be broadcastable to the same shape. Would that suffice, or should I re-word that?

Ben Root

···

On Tue, May 31, 2011 at 12:58 PM, Eric Firing <efiring@…878…202…> wrote:

On 05/31/2011 05:50 AM, Benjamin Root wrote:

On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > > > <anthony.j.mannucci@…369… > > mailto:anthony.j.mannucci@...369...> wrote:

The following program seems to work with contour/contourf. However
the documentation for the contourf function states
contour(X,Y,Z)
"/X/, /Y/, and /Z/ must be arrays with the same dimensions."
I am finding that contour works if the dimension of X and Y are 1,
but Z must be two-dimensional. The following program seems to bear
this out. Are the arrays x and y below two-dimensional, or is the
documentation misleading? Thanks for your help.
import numpy as N
import pylab as PLT
lons = N.linspace(-5.,5.,5) # Is this a one or two dimensional array?
lats = N.linspace(-3.,3.,4)
z = N.zeros((len(lats), len(lons)))
for i in range(len(lons)):
     for j in range(len(lats)):
         z[j,i]=i+j
PLT.clf()
PLT.contourf(lons,lats,z)
PLT.colorbar()
PLT.show()
-Tony

Tony,

contour and contourf seems to take advantage of numpy’s broadcasting

feature, so it is probably more correct to say that X and Y must be at

least broadcastable to the shape of Z. I think there are a number of

Not quite; if x and y are 1-D, meshgrid is called to make 2-D versions,

which must then match Z. Broadcasting is not used or supported. So, the

contour docstring was not updated when this functionality was added,

long ago. Consider it an undocumented feature, in need of documentation.

Eric

     >
     > The following program seems to work with contour/contourf.
    However
     > the documentation for the contourf function states
     >
     > contour(X,Y,Z)
     >
     > "/X/, /Y/, and /Z/ must be arrays with the same dimensions."
     >
     > I am finding that contour works if the dimension of X and Y
    are 1,
     > but Z must be two-dimensional. The following program seems to
    bear
     > this out. Are the arrays x and y below two-dimensional, or is the
     > documentation misleading? Thanks for your help.
     >
     > import numpy as N
     > import pylab as PLT
     >
     > lons = N.linspace(-5.,5.,5) # Is this a one or two
    dimensional array?
     > lats = N.linspace(-3.,3.,4)
     >
     > z = N.zeros((len(lats), len(lons)))
     > for i in range(len(lons)):
     > for j in range(len(lats)):
     > z[j,i]=i+j
     >
     > PLT.clf()
     > PLT.contourf(lons,lats,z)
     > PLT.colorbar()
     > PLT.show()
     >
     > -Tony
     >
     > Tony,
     >
     > contour and contourf seems to take advantage of numpy's broadcasting
     > feature, so it is probably more correct to say that X and Y must
    be at
     > least broadcastable to the shape of Z. I think there are a number of

    Not quite; if x and y are 1-D, meshgrid is called to make 2-D versions,
    which must then match Z. Broadcasting is not used or supported. So, the
    contour docstring was not updated when this functionality was added,
    long ago. Consider it an undocumented feature, in need of
    documentation.

    Eric

Well, (as a bit of a cop-out) in my edit, I didn't say that they were
broadcasted, only that they must be broadcastable to the same shape.
Would that suffice, or should I re-word that?

It would not be correct.

x and y must both be 2-D, with the same shape as z; or they must both be 1-D such that len(x) is the number of columns in z and len(y) is the number of rows.

Eric

···

On 05/31/2011 08:03 AM, Benjamin Root wrote:

On Tue, May 31, 2011 at 12:58 PM, Eric Firing <efiring@…202… > <mailto:efiring@…202…>> wrote:
    On 05/31/2011 05:50 AM, Benjamin Root wrote:
     > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > > <anthony.j.mannucci@…369… > <mailto:anthony.j.mannucci@…369…> > > <mailto:anthony.j.mannucci@…369… > <mailto:anthony.j.mannucci@…369…>>> wrote:

Ben Root

Gotcha, I didn’t think about the mixed 1-D and 2-D case.

In addition, is the note in the contour doc about masked arrays still valid, or can this be removed/updated?

Z may be a masked array, but filled contouring may not handle internal masked regions correctly.”

Ben Root

···

On Tue, May 31, 2011 at 1:28 PM, Eric Firing <efiring@…83…202…> wrote:

On 05/31/2011 08:03 AM, Benjamin Root wrote:

On Tue, May 31, 2011 at 12:58 PM, Eric Firing <efiring@…202… > > mailto:efiring@...120.....202...> wrote:

On 05/31/2011 05:50 AM, Benjamin Root wrote:

 >

 >

 > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G)

 > <anthony.j.mannucci@...369...

<mailto:anthony.j.mannucci@...369...>

 > <mailto:anthony.j.mannucci@...369...

<mailto:anthony.j.mannucci@...369...>>> wrote:

 >

 >     The following program seems to work with contour/contourf.

However

 >     the documentation for the contourf function states

 >

 >     contour(X,Y,Z)

 >

 > "/X/, /Y/, and /Z/ must be arrays with the same dimensions."

 >

 >     I am finding that contour works if the dimension of X and Y

are 1,

 >     but Z must be two-dimensional. The following program seems to

bear

 >     this out. Are the arrays x and y below two-dimensional, or is the

 >     documentation misleading? Thanks for your help.

 >

 >     import numpy as N

 >     import pylab as PLT

 >

 >     lons = N.linspace(-5.,5.,5) # Is this a one or two

dimensional array?

 >     lats = N.linspace(-3.,3.,4)

 >

 >     z = N.zeros((len(lats), len(lons)))

 >     for i in range(len(lons)):

 >          for j in range(len(lats)):

 >              z[j,i]=i+j

 >

 >     PLT.clf()

 >     PLT.contourf(lons,lats,z)

 >     PLT.colorbar()

 >     PLT.show()

 >

 >     -Tony

 >

 >

 > Tony,

 >

 > contour and contourf seems to take advantage of numpy's broadcasting

 > feature, so it is probably more correct to say that X and Y must

be at

 > least broadcastable to the shape of Z.  I think there are a number of



Not quite; if x and y are 1-D, meshgrid is called to make 2-D versions,

which must then match Z. Broadcasting is not used or supported. So, the

contour docstring was not updated when this functionality was added,

long ago.  Consider it an undocumented feature, in need of

documentation.



Eric

Well, (as a bit of a cop-out) in my edit, I didn’t say that they were

broadcasted, only that they must be broadcastable to the same shape.

Would that suffice, or should I re-word that?

It would not be correct.

x and y must both be 2-D, with the same shape as z; or they must both be 1-D such that len(x) is the number of columns in z and len(y) is the number of rows.

Eric

         >
         > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G)
         > <anthony.j.mannucci@…369…
        <mailto:anthony.j.mannucci@…369…>
        <mailto:anthony.j.mannucci@…369…
        <mailto:anthony.j.mannucci@…369…>>
         > <mailto:anthony.j.mannucci@…369…
        <mailto:anthony.j.mannucci@…369…>
        <mailto:anthony.j.mannucci@…369…
         >
         > The following program seems to work with contour/contourf.
            However
         > the documentation for the contourf function states
         >
         > contour(X,Y,Z)
         >
         > "/X/, /Y/, and /Z/ must be arrays with the same dimensions."
         >
         > I am finding that contour works if the dimension of X and Y
            are 1,
         > but Z must be two-dimensional. The following program seems to
            bear
         > this out. Are the arrays x and y below two-dimensional,
        or is the
         > documentation misleading? Thanks for your help.
         >
         > import numpy as N
         > import pylab as PLT
         >
         > lons = N.linspace(-5.,5.,5) # Is this a one or two
            dimensional array?
         > lats = N.linspace(-3.,3.,4)
         >
         > z = N.zeros((len(lats), len(lons)))
         > for i in range(len(lons)):
         > for j in range(len(lats)):
         > z[j,i]=i+j
         >
         > PLT.clf()
         > PLT.contourf(lons,lats,z)
         > PLT.colorbar()
         > PLT.show()
         >
         > -Tony
         >
         > Tony,
         >
         > contour and contourf seems to take advantage of numpy's
        broadcasting
         > feature, so it is probably more correct to say that X and Y must
            be at
         > least broadcastable to the shape of Z. I think there are a
        number of

            Not quite; if x and y are 1-D, meshgrid is called to make
        2-D versions,
            which must then match Z. Broadcasting is not used or
        supported. So, the
            contour docstring was not updated when this functionality
        was added,
            long ago. Consider it an undocumented feature, in need of
            documentation.

            Eric

        Well, (as a bit of a cop-out) in my edit, I didn't say that they
        were
        broadcasted, only that they must be broadcastable to the same shape.
        Would that suffice, or should I re-word that?

    It would not be correct.

    x and y must both be 2-D, with the same shape as z; or they must
    both be 1-D such that len(x) is the number of columns in z and
    len(y) is the number of rows.

    Eric

Gotcha, I didn't think about the mixed 1-D and 2-D case.

In addition, is the note in the contour doc about masked arrays still
valid, or can this be removed/updated?

"*Z* may be a masked array, but filled contouring may not handle
internal masked regions correctly."

Good catch. Ian Thomas fixed the contouring algorithm so that it handles masked regions perfectly.

Eric

···

On 05/31/2011 08:37 AM, Benjamin Root wrote:

On Tue, May 31, 2011 at 1:28 PM, Eric Firing <efiring@…202… > <mailto:efiring@…202…>> wrote:
    On 05/31/2011 08:03 AM, Benjamin Root wrote:
        On Tue, May 31, 2011 at 12:58 PM, Eric Firing > <efiring@…202… <mailto:efiring@…202…> > <mailto:efiring@…202…>> wrote:
            On 05/31/2011 05:50 AM, Benjamin Root wrote:
        <mailto:anthony.j.mannucci@…369…>>>> wrote:

Ben Root

When did that happen? I can make it a “versionadded” note so that users of older versions won’t be confused.

Ben Root

···

On Tue, May 31, 2011 at 2:38 PM, Eric Firing <efiring@…83…202…> wrote:

On 05/31/2011 08:37 AM, Benjamin Root wrote:

On Tue, May 31, 2011 at 1:28 PM, Eric Firing <efiring@…202… > > mailto:efiring@...120.....202...> wrote:

On 05/31/2011 08:03 AM, Benjamin Root wrote:







    On Tue, May 31, 2011 at 12:58 PM, Eric Firing > >  > >         <efiring@...202... <mailto:efiring@...202...> > > <mailto:efiring@...202... <mailto:efiring@...202...>>> wrote:



        On 05/31/2011 05:50 AM, Benjamin Root wrote:

     >

     >

     > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G)

     > <anthony.j.mannucci@...369...

    <mailto:anthony.j.mannucci@...369...>

    <mailto:anthony.j.mannucci@...369...

    <mailto:anthony.j.mannucci@...369...>>

     > <mailto:anthony.j.mannucci@...369...

    <mailto:anthony.j.mannucci@...369...>

    <mailto:anthony.j.mannucci@...369...

    <mailto:anthony.j.mannucci@...369...>>>> wrote:

     >

     >     The following program seems to work with contour/contourf.

        However

     >     the documentation for the contourf function states

     >

     >     contour(X,Y,Z)

     >

     > "/X/, /Y/, and /Z/ must be arrays with the same dimensions."

     >

     >     I am finding that contour works if the dimension of X and Y

        are 1,

     >     but Z must be two-dimensional. The following program seems to

        bear

     >     this out. Are the arrays x and y below two-dimensional,

    or is the

     >     documentation misleading? Thanks for your help.

     >

     >     import numpy as N

     >     import pylab as PLT

     >

     >     lons = N.linspace(-5.,5.,5) # Is this a one or two

        dimensional array?

     >     lats = N.linspace(-3.,3.,4)

     >

     >     z = N.zeros((len(lats), len(lons)))

     >     for i in range(len(lons)):

     >          for j in range(len(lats)):

     >              z[j,i]=i+j

     >

     >     PLT.clf()

     >     PLT.contourf(lons,lats,z)

     >     PLT.colorbar()

     >     PLT.show()

     >

     >     -Tony

     >

     >

     > Tony,

     >

     > contour and contourf seems to take advantage of numpy's

    broadcasting

     > feature, so it is probably more correct to say that X and Y must

        be at

     > least broadcastable to the shape of Z.  I think there are a

    number of



        Not quite; if x and y are 1-D, meshgrid is called to make

    2-D versions,

        which must then match Z. Broadcasting is not used or

    supported. So, the

        contour docstring was not updated when this functionality

    was added,

        long ago.  Consider it an undocumented feature, in need of

        documentation.



        Eric





    Well, (as a bit of a cop-out) in my edit, I didn't say that they

    were

    broadcasted, only that they must be broadcastable to the same shape.

    Would that suffice, or should I re-word that?





It would not be correct.



x and y must both be 2-D, with the same shape as z; or they must

both be 1-D such that len(x) is the number of columns in z and

len(y) is the number of rows.



Eric

Gotcha, I didn’t think about the mixed 1-D and 2-D case.

In addition, is the note in the contour doc about masked arrays still

valid, or can this be removed/updated?

"Z may be a masked array, but filled contouring may not handle

internal masked regions correctly."

Good catch. Ian Thomas fixed the contouring algorithm so that it handles masked regions perfectly.

Eric

    Good catch. Ian Thomas fixed the contouring algorithm so that it
    handles masked regions perfectly.

    Eric

When did that happen? I can make it a "versionadded" note so that users
of older versions won't be confused.

It was about 15 months ago. I think it is better to just delete any reference to the problem.

Eric

···

Ben Root

Benjamin,

Thanks. I will point out that the contourf in Basemap behaves differently than that contourf in pylab. That is, in pylab, for

contour(X,Y,Z)

X,Y can be 1D whereas Z must be 2D.

However, it appears that for contourf in Basemap:

from mpl_toolkits.basemap import Basemap

m=Basemap()

g=m.contourf(X,Y,Z)

X,Y and Z must all be 2D.

-Tony

···

Tony Mannucci

Supervisor, Ionospheric and Atmospheric Remote Sensing Group

Mail-Stop 138-308, Tel > (818) 354-1699

Jet Propulsion Laboratory, Fax > (818) 393-5115

California Institute of Technology, Email > Tony.Mannucci@…369…

4800 Oak Grove Drive, http://genesis.jpl.nasa.gov

Pasadena, CA 91109

From: Benjamin Root <ben.root@…1304…>
Date: Tue, 31 May 2011 08:50:22 -0700
To: Tony Mannucci <Anthony.J.Mannucci@…369…>
Cc: "matplotlib-users@lists.sourceforge.net" matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Input array dimensions for contour or contourf

On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) <anthony.j.mannucci@…369…> wrote:

The following program seems to work with contour/contourf. However the documentation for the contourf function states

contour(X,Y,Z)

X, Y, and Z must be arrays with the same dimensions.”

I am finding that contour works if the dimension of X and Y are 1, but Z must be two-dimensional. The following program seems to bear this out. Are the arrays x and y below two-dimensional, or is the documentation misleading? Thanks for your help.

import numpy as N

import pylab as PLT

lons = N.linspace(-5.,5.,5) # Is this a one or two dimensional array?

lats = N.linspace(-3.,3.,4)

z = N.zeros((len(lats), len(lons)))

for i in range(len(lons)):

for j in range(len(lats)):

z[j,i]=i+j

PLT.clf()

PLT.contourf(lons,lats,z)

PLT.colorbar()

PLT.show()

-Tony

Tony,

contour and contourf seems to take advantage of numpy’s broadcasting feature, so it is probably more correct to say that X and Y must be at least broadcastable to the shape of Z. I think there are a number of functions where this may or may not be true, and at some point we (the developers) should agree on basic input array handling and make it consistent across all plotting functions.

So, technically speaking, the docs are “right”, but should be clearer in this case. I will add it to my doc-fixing commit that I will do today.

Ben Root