2011-01-26 90 views
4

这是MATLAB版本的3D绘图代码: 编辑: 这是当前的代码:如何在Python中制作3D图?

 plt.figure(2) 
     fig_b = Axes3D(fig2) 
     xx2 = np.arange(0, L+h_grid*L, h_grid*L) 
     yy2 = np.arange(-b, b+h_grid*b, h_grid*b) 
     X, Y = np.meshgrid(xx2, yy2) 
     W = np.zeros((41,21), float) 
     mx = len(xx2)*len(yy2) 
     X = np.reshape(X, (1, mx)) 
     Y = np.reshape(Y, (1, mx)) 
     W = np.reshape(W, (1, mx)) 
     for j in range(0, mx): 
      W[0][j] = np.sin(np.pi*X[0][j]/L) 
     surf = fig_b.plot_surface(X, Y, W, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False) # This is the line number 168 
     plt.xlabel('x') 
     plt.ylabel('y') 

这是错误消息我得到:

Traceback (most recent call last): 
    File "nonhomog.py", line 247, in <module> 
    main() 
    File "nonhomog.py", line 245, in main 
    nonhomog(nu) 
    File "nonhomog.py", line 168, in nonhomog 
    surf = fig_b.plot_surface(X, Y, W, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False) 
    File "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/axes3d.py", line 618, in plot_surface 
    polyc = art3d.Poly3DCollection(polys, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/art3d.py", line 290, in __init__ 
    PolyCollection.__init__(self, verts, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.6/matplotlib/collections.py", line 668, in __init__ 
    self.set_verts(verts, closed) 
    File "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/art3d.py", line 312, in set_verts 
    self.get_vector(verts) 
    File "/usr/lib/pymodules/python2.6/mpl_toolkits/mplot3d/art3d.py", line 305, in get_vector 
    xs, ys, zs = zip(*points) 
ValueError: need more than 0 values to unpack 
+0

如果您要显示所有代码,包括`matplotlib`和`numpy`的导入,它将会有所帮助。 – 2011-01-26 10:02:59

+0

我已根据您展示的pastebin代码更新了我的答案。 – 2011-01-26 11:09:19

回答

4

为X和Y设置网格网格后,您需要为Z值创建一个网格。

我目前做这在我的代码的方式是:

# [ (x1, y1, z1), (x2, y2, z2), ... (xN, yN, zN) ] 
all_vals = ... 
# (x1, x2, ... xN) , (y1, y2, ... yN) , (z1, z2, ... zN) 
all_xvals, all_yvals, all_zvals = zip(*all_vals) 
fig = plt.figure() 
ax = Axes3D(fig) 
X, Y = np.meshgrid(xvals, yvals) 
# This is the part you want: 
Z1 = np.zeros(X.shape, float) 
for (x, y, z) in all_vals: 
    x = find_in_sorted_list(x, xvals) 
    y = find_in_sorted_list(y, yvals) 
    Z1[y,x] = z  
surf = ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap=cm.jet, 
     linewidth=0, antialiased=False) 
plt.xlabel('Blur standard deviation') 
plt.ylabel('JPEG quality') 
ax.w_zaxis.set_major_locator(LinearLocator(10)) 
ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))  
fig.colorbar(surf, shrink=0.5, aspect=5)  
plt.show() 

这给了我一个情节,看起来像这样:

surf

我已经保存为一个文件,但是当您拨打plt.show()时,您会看到一个交互式窗口,您可以将视角更改为任何您想要的内容。

4

有什么不对?你正在试图使一个非数字否定。换言之:AxesSubplot(不管那是什么)不实现一元运算符。

因此,该代码不能合理地“你做了什么”,因为你甚至没有在该代码中定义b,但它存在并且是一种名为AxesSubplot的自定义类型。如果你解释AxesSubplot是什么,那么这将有所帮助。如果可能的话,尝试使用包含实际演示问题的代码。

编辑: 正如DSM指出的那样,您会覆盖您的b变量。 问题是,你被卡在“数学模式”,并使用非描述性的变量名称,如“a”,“b”和“M”。改用更长的描述性名称。

相反的:

a = fig.add_subplot(2,2,i) 
b = fig2.add_subplot(2,2,i) 

做:

x_subplot = fig.add_subplot(2,2,i) 
y_subplot = fig2.add_subplot(2,2,i) 

或者类似的东西(我不知道什么是变量实际上,所以这只是一个例子)。

+0

对不起。 b是一个浮动。 http://pastebin.com/9TB02WQz – user569474 2011-01-26 08:05:42