Contour and Vector Plots

Thanks, that would be helpful. In my search I didn't come

    > across many. Keep in mind the license needs to be
    > compatible with that of matplotlib.

    >> The second half is just the drawing, which should be
    >> implemented in matplotlib using the line collections class.
    >> Since vector plotting is not

    > Yeah, that's what we have in mind.

    >> that hard, I will try to get that working first. Then, someone
    >> can take my source code and adapt it easily to the contouring
    >> problem, once an effective and sufficiently high-performance
    >> algorithm implementation can be found.

Many moons ago Helge Avlesen (CCd on this email) sent me a contouring
routine for inclusion in matplotlib. I was too busy to get to it for
a while, and when I did look at it, I was concerned by the fact that
the lines were not smooth - if you plot a connected line they line
jumps from side to side. But it does get the contour right, and is
implemented in pure numeric, and so it occurs to me that it might be
easier to fix this problem than start from scratch. Perhaps Helge or
one of you has some insight into how to fix this.

I'm attaching a modified version of the tarfile Helge initially sent
me. I've included a script testkont_mpl.py that calls Helge's lib.
Change the '.' linestyle to '-' to see the problem I discussed.

kontur.tar.gz (3.58 KB)

John Hunter <jdhunter@...5...> writes:

I was concerned by the fact that the lines were not smooth - if you
plot a connected line they line jumps from side to side. But it
does get the contour right, and is implemented in pure numeric, and
so it occurs to me that it might be easier to fix this problem than
start from scratch. Perhaps Helge or one of you has some insight
into how to fix this.

I'm attaching a modified version of the tarfile Helge initially sent
me. I've included a script testkont_mpl.py that calls Helge's lib.
Change the '.' linestyle to '-' to see the problem I discussed.

Hi,
not sure if I have matplotlib 100% correctly installed, but this is
what I see using your example script:

http://www.ii.uib.no/~avle/mpl/c0.png

(and with the current algorithm, more or less what I would expect...)
to get straight lines you must plot segments one by one since they are
not ordered. if I use gist for this(see the script at the end) I get

http://www.ii.uib.no/~avle/mpl/c1.png

the first points of the segments are given by the vectors (x1,y1) the
second (x2,y2). you can get pretty lines in matplotlib as well, but
only by using the scattered line drawing methods of gtk. (something
like self.area.window.draw_segments(self.gc, zip( x1,y1,x2,y2)?)

if you want do do it "right" in matplotlib, you should implement a
contour following algorithm (in C) - with this I mean an routine that
returns the linesegments defining each countour in bundles. the
current alg. is sort of marching cubes in 2D, a simplified version of
CONREC
http://astronomy.swin.edu.au/~pbourke/projection/conrec/
but only using 2 triangles per square.

doing contour following alg. it is also much easier to implement
automatic contour labelling. I suspect python loops are too slow for
such algorithms - it may perhaps be possible to do them in Numeric,
but it will still be much slower than my simple library. I think you
may use the GPL'ed PLPLOT (C) for an example of contour following alg.

Helge

from matplotlib.matlab import *
import hutil

delta = 0.05
x = y = arange(-3.0, 3.0, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2-Z1

print Z.shape
#fsm = ones(Z.shape, Z.typecode())
fsm = ones(Z.shape, 'l')

zmax, zmin = hutil.maxmin(Z)
depths=linspace(zmin, zmax, 10)

x1,y1,x2,y2 = hutil.contour2(Z, fsm, depths )

#imshow(Z, origin='lower', interpolation='nearest')
#plot(y2,x2,'-')
#show()

import gist
gist.pldefault(dpi=100,style='framed.gs')
gist.palette('rainbow.gp')
gist.pli(transpose(Z))
gist.pldj(x1,y1,x2,y2) # draw disjoint segments