469,945 Members | 2,341 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

how to use matplotlib contour()?

I downloaded examples/contour_demo.py, and it doesn't run.

I've searched both the user guide and the Wiki for "contour"
and got zero hits.

http://matplotlib.sourceforge.net/ma....html#-contour
appears to be a good reference if you already know how to use
contour(), but I could glean zero clues from it on how to
actually use contour(). For example, it doesn't explain what
the actual formats/types of the parameters. It explains what
the parameters do, but not what they _are_

For example one parameter is specied as "an array". No clue as
to how many dimensions or what the axis are.

In another place it says "the X,Y parameters specify the (x,y)
coordinates of a surface". _How_ do they specify the surface?
Are they just equal length lists of x and y coordinates that
specify len(X) points. Or do they specify a len(X) x len(Y)
grid of points?

Since I can't find any documentation, I thought I'd just try a
few things.

I've got X,Y,Z values in an Nx3 array named "data" that looks
like this:

X Y Z
[[ 4.94958000e+01 3.65706000e+00 1.84600000e-01]
[ 4.94958000e+01 3.66447000e+00 1.82142000e-01]
[ 4.94958000e+01 5.04936000e+00 1.90937000e-01]
...,
[ 2.19844000e+03 9.74210000e+00 8.29735000e-01]
[ 2.19844000e+03 9.93863000e+00 9.82307000e-01]
[ 2.19844000e+03 1.03143000e+01 8.28844000e-01]]

If I just pass the array to contour() by doing
"pylab.contour(data)" I get this error:

Traceback (most recent call last):
File "./surfplot.py", line 11, in ?
pylab.contour(data)
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line 1659, in contour
ret = gca().contour(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 1244, in contour
return self._contourHelper.contour(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/contour.py", line 727, in contour
x, y, z, lev = self._contour_args(False, badmask, origin, extent, *args)
File "/usr/lib/python2.4/site-packages/matplotlib/contour.py", line 544, in _contour_args
lev = self._autolev(z, 7, filled, badmask)
File "/usr/lib/python2.4/site-packages/matplotlib/contour.py", line 471, in _autolev
lev = linspace(zmin, zmax, N+2)[1:-1]
File "/usr/lib/python2.4/site-packages/matplotlib/mlab.py", line 87, in linspace
dx = (xmax-xmin)/(N-1)
TypeError: unsupported operand type(s) for -: 'str' and 'str'

I've no clue what that means. Well, I know what the TypeError
means _literally_, but I've no idea how I'm supposed to
interpret that particular error in regards to what I passed to
contour().

Attempting to use one of the other forms found on the above web
page I pass X,Y,Z vectors to contour() by doing
"pylab.contour(data[:,0],data[:,1],data[:,2])":

Traceback (most recent call last):
File "./surfplot.py", line 13, in ?
pylab.contour(data[:,0],data[:,1],data[:,2])
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line 1659, in contour
ret = gca().contour(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 1244, in contour
return self._contourHelper.contour(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/contour.py", line 727, in contour
x, y, z, lev = self._contour_args(False, badmask, origin, extent, *args)
File "/usr/lib/python2.4/site-packages/matplotlib/contour.py", line 539, in _contour_args
x,y,z = self._check_xyz(args[:3])
File "/usr/lib/python2.4/site-packages/matplotlib/contour.py", line 515, in _check_xyz
raise TypeError("Input z must be a 2D array.")
TypeError: Input z must be a 2D array.

Which is almost as cryptic a result as my first attempt.

Why would my Z values be a 2D array?

--
Grant Edwards grante Yow! My NOSE is NUMB!
at
visi.com
May 24 '06 #1
2 11357
Grant Edwards wrote:
I downloaded examples/contour_demo.py, and it doesn't run.

I've searched both the user guide and the Wiki for "contour"
and got zero hits.

http://matplotlib.sourceforge.net/ma....html#-contour
appears to be a good reference if you already know how to use
contour(), but I could glean zero clues from it on how to
actually use contour(). For example, it doesn't explain what
the actual formats/types of the parameters. It explains what
the parameters do, but not what they _are_
Yes, unfortunately, much of the documentation was written by people who were
very familiar with the Matlab interfaces that these functions are emulating.
For example one parameter is specied as "an array". No clue as
to how many dimensions or what the axis are.

In another place it says "the X,Y parameters specify the (x,y)
coordinates of a surface". _How_ do they specify the surface?
Are they just equal length lists of x and y coordinates that
specify len(X) points. Or do they specify a len(X) x len(Y)
grid of points? Why would my Z values be a 2D array?


contour() only does contouring on gridded data. If you want to handle scattered
datapoints, you will have to do some interpolation.

http://www.scipy.org/Cookbook/Matplo...ly_spaced_data

So X, Y, and Z are all 2-D arrays laid out corresponding to the grid that you
have sampled. I thought the contour_demo.py example was reasonably clear on
this, but if you didn't get it to run, then I can see why you wouldn't have read it.

Talking about this on matplotlib-users will probably get these problems fixed
faster:

https://lists.sourceforge.net/lists/...tplotlib-users

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 24 '06 #2
On 2006-05-24, Robert Kern <ro*********@gmail.com> wrote:
Yes, unfortunately, much of the documentation was written by people who were
very familiar with the Matlab interfaces that these functions are emulating.
Since I've never used matlab, I'm a bit clueless.
For example one parameter is specied as "an array". No clue as
to how many dimensions or what the axis are.

In another place it says "the X,Y parameters specify the (x,y)
coordinates of a surface". _How_ do they specify the surface?
Are they just equal length lists of x and y coordinates that
specify len(X) points. Or do they specify a len(X) x len(Y)
grid of points?

Why would my Z values be a 2D array?


contour() only does contouring on gridded data.


That's what I was beginning to suspect. What confused me was
that if it required gridded data, I expected the input parameters to
specify a grid (e.g. for a 5x7 grid, the X parameter would be a
vector of 5 values, and the Y parameter would be a vector of 7
values) rather than just lie on a grid.
If you want to handle scattered datapoints, you will have to
do some interpolation.

http://www.scipy.org/Cookbook/Matplo...ly_spaced_data
Thanks, that looks like exactly what I need. My next step was
actually going to be to use the Delaunay triangulation module
to interpolate the data onto a much finer grid.
So X, Y, and Z are all 2-D arrays laid out corresponding to
the grid that you have sampled. I thought the contour_demo.py
example was reasonably clear on this, but if you didn't get it
to run, then I can see why you wouldn't have read it.
I did delete some code that was attempting to label the graph,
and then it ran. The examples do use gridded data, but when I
changed them to non-gridded data, it seemed to run fine.
Talking about this on matplotlib-users will probably get these
problems fixed faster:

https://lists.sourceforge.net/lists/...tplotlib-users


After I got the demos to run, it became apparent that the
contour functions don't do what I want anyway, so it's all moot
at this point.

--
Grant Edwards grante Yow! YOW!! Now I
at understand advanced
visi.com MICROBIOLOGY and th' new
TAX REFORM laws!!
May 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by John Hunter | last post: by
3 posts views Thread by John Hunter | last post: by
2 posts views Thread by scott | last post: by
reply views Thread by spross | last post: by
reply views Thread by Soren | last post: by
4 posts views Thread by Bill Jackson | last post: by
4 posts views Thread by John Henry | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.