我有一个形式如下的函数:
def f(x, y):
total = 0
u = np.zeros(10)
for i in range(0,10):
u[i] = x * i + y* i
if u[i] < 10:
print('do something')
total = total + u[i]
return total
当我尝试使用给定的x和y值时,此功能效果很好.
f(3,4)
Out[49]: 63.0
我想使用matplotlib创建3d轮廓图.尝试过
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
我必须为3d图创建一个网格.尝试此操作时,由于函数中的循环,出现错误.我得到一个错误
ValueError: setting an array element with a sequence.
如果我的函数有循环,如何绘制3D图形?