matplotlib pyplot contourf with 1-D array (vector)

B

becky_s

All,

I’m having a problem with the matplotlib.pyplot.contourf function. I
have a 1-D array of latitudes (mesolat), a 1-D array of longitudes
(mesolon), and a 1-D array of rainfall values (rain) at those
corresponding lat, lon points. After importing the necessary
libraries, and reading in these 1-D arrays, I do the following
commands:

p = Basemap(projection='lcc',llcrnrlon=-108.173,llcrnrlat=26.809999,
urcrnrlon=-81.944664,urcrnrlat=45.730892,
lon_0=-97.00, lat_0=37.00, resolution='i')

px,py = p(mesolon, mesolat)

prplvls = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

crain = p.contourf(px,py,rain,prplvls)


At this point the contourf function returns an error saying “Input z
must be a 2D array.” However, based on the documentation (http://
matplotlib.sourceforge.net/api/
pyplot_api.html#matplotlib.pyplot.contourf) I thought that as long as
px, py, and rain are the same dimensions, everything should be fine.
Apparently that is not the case? If 1D arrays are not allowed in
contourf, then how can I change my data into a 2D array?


Thanks in advance for the help.
 
B

becky_s

All,

I’m having a problem with the matplotlib.pyplot.contourf function.  I
have a 1-D array of latitudes (mesolat), a 1-D array of longitudes
(mesolon), and a 1-D array of rainfall values (rain) at those
corresponding lat, lon points.  After importing the necessary
libraries, and reading in these 1-D arrays, I do the following
commands:

p = Basemap(projection='lcc',llcrnrlon=-108.173,llcrnrlat=26.809999,
            urcrnrlon=-81.944664,urcrnrlat=45.730892,
            lon_0=-97.00, lat_0=37.00, resolution='i')

px,py = p(mesolon, mesolat)

prplvls = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

crain = p.contourf(px,py,rain,prplvls)

At this point the contourf function returns an error saying “Input z
must be a 2D array.”  However, based on the documentation (http://
matplotlib.sourceforge.net/api/
pyplot_api.html#matplotlib.pyplot.contourf) I thought that as long as
px, py, and rain are the same dimensions, everything should be fine.
Apparently that is not the case?  If 1D arrays are not allowed in
contourf, then how can I change my data into a 2D array?

Thanks in advance for the help.

I neglected to mention that these are masked arrays, due to some
missing data. I tried using numpy.griddata:

mesolati = np.linspace(33.8,37.0,150)
mesoloni = np.linspace(-94.5,-102.9,150)
raini = griddata(mesolon,mesolat,rain,mesoloni,mesolati)

but the raini array returned was entirely masked (no values).

Thanks again, Becky
 
D

Dennis Lee Bieber

px,py = p(mesolon, mesolat)
For my elucidation, what does that p(x,y) actually do? Especially
as you appear to expect the result to be split into separate x and y
afterwards? I can't find it defined in either matplotlib nor numpy.

pyplot_api.html#matplotlib.pyplot.contourf) I thought that as long as
px, py, and rain are the same dimensions, everything should be fine.

And are they? You don't demonstrate that you've checked for that
Apparently that is not the case? If 1D arrays are not allowed in
contourf, then how can I change my data into a 2D array?
Also note (jumping to your follow up) that contourf is described as
having a potential problem with masked arrays (whatever those are) for
"Z"
 
B

becky_s

        For my elucidation, what does that      p(x,y)  actually do? Especially
as you appear to expect the result to be split into separate x and y
afterwards? I can't find it defined in either matplotlib nor numpy.

p was declared as a basemap object in the previous statement, which
sets up a map projection. Calling p(mesolon, mesolat) converts those
lons, lats to units in that map projection and stores them in px,py.
(See http://matplotlib.sourceforge.net/matplotlib.toolkits.basemap.basemap.html
for more info on basemap.)

        And are they? You don't demonstrate that you've checked for that

I did a simple print px.shape, rain.shape, etc. to check. They are
all size (135,).

        Also note (jumping to your follow up) that contourf is described as
having a potential problem with masked arrays (whatever those are) for
"Z"


My problem isn't with the masked arrays as much as it is with rain
being a 1D array. IDL can handle contouring 1D arrays with missing
variables lickety-split, so I was really hoping Python could as well.

Also, numpy.ma is the masked array library. See
http://docs.scipy.org/doc/numpy/reference/routines.ma.html.
 
B

becky_s

        For my elucidation, what does that      p(x,y)  actually do? Especially
as you appear to expect the result to be split into separate x and y
afterwards? I can't find it defined in either matplotlib nor numpy.

p was declared as a basemap object in the previous statement, which
sets up a map projection. Calling p(mesolon, mesolat) converts those
lons, lats to units in that map projection and stores them in px,py.
(See http://matplotlib.sourceforge.net/matplotlib.toolkits.basemap.basemap.html
for more info on basemap.)

        And are they? You don't demonstrate that you've checked for that

I did a simple print px.shape, rain.shape, etc. to check. They are
all size (135,).

        Also note (jumping to your follow up) that contourf is described as
having a potential problem with masked arrays (whatever those are) for
"Z"


My problem isn't with the masked arrays as much as it is with rain
being a 1D array. IDL can handle contouring 1D arrays with missing
variables lickety-split, so I was really hoping Python could as well.

Also, numpy.ma is the masked array library. See
http://docs.scipy.org/doc/numpy/reference/routines.ma.html.
 
B

becky_s

I was able to figure this out on my own. First, to eliminate the
masked arrays, I used a combination of the where and compress
functions to remove any missing data from my 1-D arrays. Then, I used
the griddata function as described above. This did the trick.
 
D

Dennis Lee Bieber

p was declared as a basemap object in the previous statement, which

Ah, guess I missed that (suspect I was reading bottom up trying to
trace what was coming from where).
I did a simple print px.shape, rain.shape, etc. to check. They are
all size (135,).
Spent today at work browsing documentation... And what I found could
easily signify that "Z" had to be a 2D array... Since you have 135 rows
by 135 columns defined by x and y. Passing a vector of only 135 elements
seems to leave a lot of undefined data...

I'll still have to confess that I don't have experience with the
rest of matplotlib/numpy (they're installed, but I've not needed to use
them -- I have a smidgen more experience [and lots more documentation]
for R). As a result, the only way I could see a vector being used for Z
is if it has 135*135 elements, and the plotter wraps it into rows (or
columns) as needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top