0

I want to draw the contourf of a certain function and my code was as follows:

xlist = linspace(0, 100, 100)
ylist = linspace(0, 100, 200)
X, Y = meshgrid(xlist, ylist)
#print "X = " + str(X)
#print "Y = " + str(Y)
Z = power_at_each_point(X, Y)
#print "Z = " + str(Z)
figure()
CP2 = contourf(X, Y, Z)
colorbar(CP2)
title('Contour Plot')
xlabel('Room-x (m)')
ylabel('Room-y (m)')
show()

The function power_at_each_point(X,Y) when I test it alone I write:

print power_at_each_point(50, 50)

and the output is -80.9187477018

which basically represents the power reached to this point in the room and it outputs a number normally but when I call it after the meshgrid command it returns an error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I want to take each coordinate of points in the room x-coord and y-coord and calculate the power reached at this point using the power_at_each_point method which is supposed to return a number and I'd represent it in the contourf plot.

My guess is that the arguments (X,Y) of Z = power_at_each_point changed from being just numbers to being arrays which I don't want and that's what is causing the error. How can I let the function Z = power_at_each_point(X,Y) take as arguments X as a number ex :1 and Y ex :2 and return a value for the power at this point so that I can represent it in my contourf plot.

Any help would be appreciated.

Mahmoud Ayman
  • 197
  • 1
  • 13
  • The problem is probably with your function `power_at_each_point` which doesn't know what to do with non-scalar arguments. Perhaps you should post the code to this function. – xnx May 04 '15 at 13:29

1 Answers1

0

I've found that the function meshgrid wants a Matrix pretty much as an argument so I went ahead and created a Matrix called Z and I filled by myself the values in it and then I went ahead and entered Z as an argument to the meshgrid function:

x_list = linspace(0, 100, 100)
y_list = linspace(0, 100, 100)
X, Y = meshgrid(x_list, y_list)

Z = [[0 for x in range(len(x_list))] for x in range(len(y_list))]
for each_axes in range(len(Z)):
    for each_point in range(len(Z[each_axes])):
        Z[each_axes][each_point] = power_at_each_point(each_axes, each_point)

figure()
CP2 = contourf(X, Y, Z)

and that got me the result I wanted as far as I was asking here. The points is reversed in the Z Matrix or as you can say mirrored along the horizontal but that's something i'm gonna play with so that I can get those elements in Z to match how the meshgrid actually sets it's grid points.

Mahmoud Ayman
  • 197
  • 1
  • 13