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?
Thank you in advance.

#! /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[0]
xmax=x[len(x)-1]
ymin=y[0]
ymax=y[len(y)-1]

#ax.set_xlim(xmin,xmax)
#ax.set_ylim(ymin,ymax)
#axes().set_aspect('auto',adjustable='box')

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[0] + 1, y[-1] - y[0] + 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[0], yv - y[0]] = 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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top