## Re: [Matplotlib-users] Input array dimensions for contour or contourf

 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Benjamin Root - 2011-05-31 18:37:56 Attachments: Message as HTML ```On Tue, May 31, 2011 at 1:28 PM, Eric Firing wrote: > On 05/31/2011 08:03 AM, Benjamin Root wrote: > >> >> >> On Tue, May 31, 2011 at 12:58 PM, Eric Firing > > wrote: >> >> On 05/31/2011 05:50 AM, Benjamin Root wrote: >> > >> > >> > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) >> > > >> > > >> 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." Ben Root ```

### Thread view

 [Matplotlib-users] Input array dimensions for contour or contourf From: Mannucci, Anthony J (335G) - 2011-05-31 15:35:12 Attachments: Message as HTML ```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@... 4800 Oak Grove Drive, http://genesis.jpl.nasa.gov Pasadena, CA 91109 ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Benjamin Root - 2011-05-31 15:50:51 Attachments: Message as HTML ```On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) < anthony.j.mannucci@...> 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 ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Mannucci, Anthony J (335G) - 2011-06-06 15:52:17 Attachments: Message as HTML ```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@... 4800 Oak Grove Drive, http://genesis.jpl.nasa.gov Pasadena, CA 91109 From: Benjamin Root > Date: Tue, 31 May 2011 08:50:22 -0700 To: Tony Mannucci > Cc: "matplotlib-users@..." > Subject: Re: [Matplotlib-users] Input array dimensions for contour or contourf On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > 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 ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Eric Firing - 2011-05-31 17:58:19 ```On 05/31/2011 05:50 AM, Benjamin Root wrote: > > > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > > 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 > 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@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Benjamin Root - 2011-05-31 18:04:13 Attachments: Message as HTML ```On Tue, May 31, 2011 at 12:58 PM, Eric Firing wrote: > On 05/31/2011 05:50 AM, Benjamin Root wrote: > > > > > > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > > > > 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? Ben Root ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Eric Firing - 2011-05-31 18:28:53 ```On 05/31/2011 08:03 AM, Benjamin Root wrote: > > > On Tue, May 31, 2011 at 12:58 PM, Eric Firing > wrote: > > On 05/31/2011 05:50 AM, Benjamin Root wrote: > > > > > > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > > > > >> 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 > > Ben Root > ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Benjamin Root - 2011-05-31 18:37:56 Attachments: Message as HTML ```On Tue, May 31, 2011 at 1:28 PM, Eric Firing wrote: > On 05/31/2011 08:03 AM, Benjamin Root wrote: > >> >> >> On Tue, May 31, 2011 at 12:58 PM, Eric Firing > > wrote: >> >> On 05/31/2011 05:50 AM, Benjamin Root wrote: >> > >> > >> > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) >> > > >> > > >> 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." Ben Root ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Eric Firing - 2011-05-31 19:39:02 ```On 05/31/2011 08:37 AM, Benjamin Root wrote: > > > On Tue, May 31, 2011 at 1:28 PM, Eric Firing > wrote: > > On 05/31/2011 08:03 AM, Benjamin Root wrote: > > > > On Tue, May 31, 2011 at 12:58 PM, Eric Firing > > >> wrote: > > On 05/31/2011 05:50 AM, Benjamin Root wrote: > > > > > > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) > > > > > > > >>> 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 > > Ben Root > ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Benjamin Root - 2011-05-31 19:54:37 Attachments: Message as HTML ```On Tue, May 31, 2011 at 2:38 PM, Eric Firing wrote: > On 05/31/2011 08:37 AM, Benjamin Root wrote: > >> >> >> On Tue, May 31, 2011 at 1:28 PM, Eric Firing > > wrote: >> >> On 05/31/2011 08:03 AM, Benjamin Root wrote: >> >> >> >> On Tue, May 31, 2011 at 12:58 PM, Eric Firing >> >> >> wrote: >> >> On 05/31/2011 05:50 AM, Benjamin Root wrote: >> > >> > >> > On Tue, May 31, 2011 at 10:34 AM, Mannucci, Anthony J (335G) >> > > >> > > >> > > >> > >>> 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 > > When did that happen? I can make it a "versionadded" note so that users of older versions won't be confused. Ben Root ```
 Re: [Matplotlib-users] Input array dimensions for contour or contourf From: Eric Firing - 2011-05-31 20:23:36 ```> > > 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 > ```