在python中使用plot3d

时间:2012-10-05 11:43:36

标签: python mplot3d

我正在解决一个优化问题,我想用plot3d工具包绘制我找到的解决方案。假设我试图找到函数F的最小值。我们调用最小化F x*的x的值。我想将F生成的曲面绘制为网格,并在同一绘图上打印F : F(x*)的最小值。

我为此目的使用了这段代码:

x = [a / 10 - 5 for a in range(100)]
y = [a / 10 - 5 for a in range(100)]

Z = [] 
for i in range(len(x)):
    Z.append([0] * len(x)) 
Z = array(Z)    
for i in range(len(x)):
    for j in range(len(x)):
        Z[i, j] = F([x[i], y[j]])
X = identity(len(x))
for i in range(len(x)):
     X[:, i] = x
Y = X.T

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2, color='blue')
ax = fig.gca(projection = '3d')
ax.plot(sol.x[0], sol.x[1], [F(sol.x)], color='green')

plt.show()  

sol.x是最小化F的值(使其变得简单sol.x=x*)。此代码返回以下错误:

Traceback (most recent call last):
File "projet2.py", line 103, in <module>
ax.plot(sol.x[0],sol.x[1],[f1(sol.x)],color='green')
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 1296, in plot
len(xs) == len(args[0]) :
TypeError: object of type 'numpy.float64' has no len()

有没有人知道解决这个问题?

由于

昆汀

0 个答案:

没有答案