绘制contourf图时的值误差

时间:2021-01-11 15:00:24

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

我想绘制某个函数的contourf,我的代码如下:

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:

我单独测试时函数power_at_each_point(X,Y)我写道:

print power_at_each_point(50, 50)

and the output is -80.9187477018

输出为-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:

这基本上代表了房间到达这一点的功率,并且它正常输出一个数字但是当我在meshgrid命令之后调用它时它会返回一个错误:

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.

我想获取房间x-coord和y-coord中每个点的坐标,并使用power_at_each_point方法计算此时达到的功率,该方法应该返回一个数字,我将在contourf图中表示它。

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.

我的猜测是Z = power_at_each_point的参数(X,Y)从只是数字变为我不想要的数组,这就是造成错误的原因。我怎样才能将函数Z = power_at_each_point(X,Y)作为参数X作为数字ex:1和Y ex:2并在此时返回一个幂的值,以便我可以在我的contourf图中表示它。

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


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:

我发现函数meshgrid想要一个矩阵作为一个参数,所以我继续创建了一个名为Z的矩阵,我自己填充了它中的值,然后我继续前进并输入Z作为meshgrid函数的参数:

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.

就我在这里问的那样,这让我得到了我想要的结果。这些点在Z矩阵中是相反的,或者你可以说是沿着水平方向镜像,但这是我要玩的东西,这样我就可以在Z中获得这些元素,以匹配meshgrid实际设置它的网格点的方式。

#1


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:

我发现函数meshgrid想要一个矩阵作为一个参数,所以我继续创建了一个名为Z的矩阵,我自己填充了它中的值,然后我继续前进并输入Z作为meshgrid函数的参数:

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.

就我在这里问的那样,这让我得到了我想要的结果。这些点在Z矩阵中是相反的,或者你可以说是沿着水平方向镜像,但这是我要玩的东西,这样我就可以在Z中获得这些元素,以匹配meshgrid实际设置它的网格点的方式。