【问题标题】:plotting 3D surface using python: raise ValueError("Argument Z must be 2-dimensional.") matplotlib [duplicate]使用 python 绘制 3D 表面:raise ValueError("Argument Z must be 2-dimensional.") matplotlib [重复]
【发布时间】:2018-07-28 20:05:28
【问题描述】:

我正在尝试绘制一个穿过一组 (X,Y,Z) 3d 点的 3d 曲面,但我得到了 raise ValueError("Argument Z must be 2-dimensional.") matplotlib

points = Tail_geo(D,L) # is a list from a function 
points = points + Nose_geo(D,L)# is a list from both function s
X = [x[0] for x in points]# seperate X from the list
Y = [x[1] for x in points]
Z = [x[2] for x in points]
X = np.asarray(X)
Y = np.asarray(Y)
Z = np.asarray(Z)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, linewidth=0, antialiased=False)
plt . show ( )

【问题讨论】:

  • 这段代码 sn-p 不完整,因为您没有显示函数 Tail_geoNose_geo 的定义,也没有显示导入。这些事情可能很重要。请显示完整的代码 sn-p,我们可以复制并粘贴然后运行。还显示错误的完整回溯。检查How to create a Minimal, Complete, and Verifiable example
  • 在您的代码中Z 似乎也是一个一维列表。在 3d 表面中,对于一组 XX 点,您需要这些 XY 点的网格,然后在每个 n 次 @987654332 处定义/计算 Z @grid(假设len(X) = n; len(Y) = n)。使用当前数据,您可以绘制的只是 3d 散点图。如果您想了解更多信息,请分享您的代码。

标签: python matplotlib


【解决方案1】:

函数plot_surface 期望其输入构造为常规二维网格。对于您的数据(x、y、z 作为列表),使用 plot_trisurf 函数可能更合适。只需在您的代码中进行简单的替换即可。

surf = ax.plot_trisurf(X, Y, Z, linewidth=0, antialiased=False)

https://matplotlib.org/examples/mplot3d/trisurf3d_demo.html 的 matplotlib 库中有一个很好的示例,您可以查看更多详细信息。

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2018-12-13
    • 2021-08-13
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多