# Drawing a surface with matplotlib

M

#### Monnin

Hello,
I have a newbie question about using matplotlib
I would like to draw the surface defined by the lists X, Y and the
matrix Z.
I get to a nice graphical output with the following code.
My problem is that the labels on the axes indicate values
corresponding to the indices in Tables X and Y.
I would like to see these values between xmin and xmax, ymin and ymax.
If I uncomment the lines set_xlim and set_ylim, the axes are properly
represented, but the picture is squashed into a corner.
How have the values on the axes and correct the image full screen?

#! /usr/bin/env python

from pylab import *

x=[0,1,2,3]
y=[12, 32,81]
z=[2,3,1,4,2,5,6,8,10,4,11,3]
Z=zeros((len(x),len(y)))
count=0
for i in range(0,len(x)):
for j in range(0,len(y)):
Z[i,j]=z[count]
count=count+1

ax = subplot(111)

im = imshow(Z, interpolation='bilinear')
xmin=x
xmax=x[len(x)-1]
ymin=y
ymax=y[len(y)-1]

#ax.set_xlim(xmin,xmax)
#ax.set_ylim(ymin,ymax)

colorbar()

show()

D

#### Dennis Lee Bieber

If I uncomment the lines set_xlim and set_ylim, the axes are properly
represented, but the picture is squashed into a corner.
How have the values on the axes and correct the image full screen?

By scaling the Z array so the subscripts match the desired axes
limits.
x=[0,1,2,3]
y=[12, 32,81]
z=[2,3,1,4,2,5,6,8,10,4,11,3]
Z=zeros((len(x),len(y)))

Z = zeros( (x[-1] - x + 1, y[-1] - y + 1) )

Results in an array of dimensions 4 by 70 elements. (Note: you do
NOT use x[len(x) - 1] to get the last element of a list; just use x[-1])

Now populate the sparse array...
for i in range(0,len(x)):
for j in range(0,len(y)):
Z[i,j]=z[count]
count=count+1

for ix, xv in enumerate(x):
for iy, yv in enumerate(y):
Z[xv - x, yv - y] = z[ix * len(y) + iy]

Of course, since this IS a sparse array, with a lot of points having
a value of 0, the surface plot may not be what you really want. Perhaps
you'll have to do a 2D interpolation to fill in all the vacant (0)
spots...

Alternatively -- there may be a command to take a list of values and
assign them as labels to the tic marks of the axes... in which case you
keep the small indices (4 by 3 array). (I have, I think, a book on order
at Amazon for this library, but in the meantime -- take a look at
chapter 5 of http://mural.uv.es/~parmur/matplotlib.pdf or maybe 2.6.1