I notice some spurious zigzagging lines towards the top

> of the plot. Any idea where those might be coming from?

I don't get zigzag lines either. Try using replacing

matplotlib.axes.Axes.contour with the function included below (from

CVS) and let me know if it works.

> Also, the figure produced by the above script is

> flipped horizontally. The corresponding Matlab script

> produces the correct plot.

matplotlib supports two image / contour orientation styles, and this

is controlled by an rc parameter. Try (note the origin kwarg)

from matplotlib.pylab import *

def rosenbrock(x,y):

return 10.0 * (y-x**2)**2 + (x-1)**2

x = arange( -1.5, 1.5, 0.01 )

y = arange( -0.5, 1.5, 0.01 )

[X,Y] = meshgrid( x, y )

Z = rosenbrock( X, Y )

contour( Z, x=X, y=Y, levels = 50, origin='lower' )

show()

Amended contour function

def contour(self, z,

x = None,

y = None,

levels = None,

colors = None,

linewidths = None,

alpha = 1.0,

fmt='%1.3f',

origin=None):

"""\

CONTOUR(z, x = None, y = None, levels = None, colors = None)

plots contour lines of an image z

z is a 2D array of image values

x and y are 2D arrays with coordinates of z values in the

two directions. x and y do not need to be evenly spaced but must

be of the same shape as z

levels can be a list of level values or the number of levels to be

plotted. If levels == None, a default number of 7 evenly spaced

levels is plotted.

colors is one of these:

- a tuple of matplotlib color args (string, float, rgb, etc),

different levels will be plotted in different colors in the order

specified

- one string color, e.g. colors = 'r' or colors = 'red', all levels

will be plotted in this color

- if colors == None, the default color for lines.color in

.matplotlibrc is used.

linewidths is one of:

- a number - all levels will be plotted with this linewidth,

e.g. linewidths = 0.6

- a tuple of numbers, e.g. linewidths = (0.4, 0.8, 1.2) different

levels will be plotted with different linewidths in the order

specified

- if linewidths == None, the default width in lines.linewidth in

.matplotlibrc is used

reg is a 2D region number array with the same dimensions as x and

y. The values of reg should be positive region numbers, and zero fro

zones wich do not exist.

triangle - triangulation array - must be the same shape as reg

alpha : the default transparency of contour lines

fmt is a format string for adding a label to each collection.

Currently this is useful for auto-legending and may be useful down

the road for legend labeling

origin = 'upper'|'lower'|None. If None, the rc value for image.origin

will be used

More information on reg and triangle arrays is in _contour.c

Return value is levels, collections where levels is a list of contour

levels used and collections is a list of

matplotlib.collections.LineCollection instances

"""

if origin is None: origin = rcParams['image.origin']

if origin == 'upper':

if x is not None: x = x[::-1]

if y is not None: y = y[::-1]

z = z[::-1]

if not self._hold: self.cla()

if which[0] == "numeric":

z = array(z.tolist(),typecode = z.typecode())

if len(shape(z)) != 2:

raise TypeError("Input must be a 2D array.")

else:

jmax, imax = shape(z)

region = 0

reg = ones((jmax,imax), Int32)

reg[:,0]=0

triangle = zeros((jmax,imax), Int32)

if x == None and y == None:

y, x = indices((jmax,imax), 'd')

rz = ravel(z)

zmax = nxmax(rz)

zmin = nxmin(rz)

def autolev(N):

return mlab.linspace(zmin, zmax, N+2)[1:-1]

if levels is None: lev = autolev(7)

else:

try: Nlev = int(levels)

except TypeError:

lev = list(levels)

else: lev = autolev(Nlev)

Nlev = len(lev)

if colors == None:

colors = rcParams['lines.color']

if is_string_like(colors):

colors = [colors] * Nlev

elif iterable(colors) and len(colors) < Nlev:

colors = list(colors) * Nlev

else:

try: gray = float(colors)

except TypeError: pass

else: colors = [gray] * Nlev

tcolors = []

for c in colors:

tcolors.append((colorConverter.to_rgba(c, alpha),))

if linewidths == None:

tlinewidths = [linewidths] *Nlev

else:

if iterable(linewidths) and len(linewidths) < Nlev:

linewidths = list(linewidths) * int(ceil(Nlev/len(linewidths)))

elif not iterable(linewidths) and type(linewidths) in [int, float]:

linewidths = [linewidths] * Nlev

tlinewidths = [(w,) for w in linewidths]

args = zip(lev, tcolors, tlinewidths)

levels = []

collections = []

for level, color, width in args:

ntotal, nparts = _contour.GcInit1(x, y, reg, triangle, region, z, level)

np = zeros((nparts,), Int32)

xp = zeros((ntotal, ), Float64)

yp = zeros((ntotal,), Float64)

nlist = _contour.GcTrace(np, xp, yp)

col = LineCollection(nlist, colors=color, linewidths = width)

col.set_label(fmt%level)

self.add_collection(col)

levels.append(level)

#col.set_linestyle('dashdot') # dashed|dotted|solid|dashdot

#dashes = 0, (4,2,8,1)

#col.set_linestyle( (dashes,) ) # offset, onoffseq

collections.append(col)

if x is not None:

rx = ravel(x)

self.set_xlim((min(rx), max(rx)))

else:

self.set_xlim([0,imax])

if y is not None:

ry = ravel(y)

self.set_ylim((min(ry), max(ry)))

else:

self.set_ylim([0,jmax])

return levels, collections