【问题标题】:plot 3d in python using three lists使用三个列表在 python 中绘制 3d
【发布时间】:2017-06-04 14:41:44
【问题描述】:

我按照link 绘制了 3D 图形。 我的问题是我已经有 3 个 X、Y、Z 列表

X.shape (n,) , Y.shape (n,) , Z.shape (n,)

如何将这些列表传递到surf = ax.plot_surface(X, Y, Z) 作为链接显示这些变量中的每一个都具有以下形状

X.shape (n,n) , Y.shape (n,n) , Z.shape (n,n)

如果我传递这些坐标,因为它们每个形状都是 (n,),那么 3d 图形将显示为空,不会绘制任何点!

我尝试如下使用np.meshgrid,但这种方式只会在一个平面上显示一个表面,而不是 3d 点!

X,Y,Z = np.meshgrid(X,Y,Z)

X = X[0]
Y = Y[0]
Z = Z[0]

fig = plt.figure()

ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()

【问题讨论】:

  • 你能告诉我们更多关于你原来的 X,Y,Z 数组的内容吗,也许它们可以被重新塑造以适应 plot_surface 的要求。

标签: python numpy matplotlib mplot3d


【解决方案1】:

解决方案将取决于数据的组织方式。

常规网格上的数据

如果XY 数据已经定义了一个网格,则可以轻松地将它们重新调整为四边形网格。例如

#x  y  z
 4  1  3
 6  1  8
 8  1 -9
 4  2 10
 6  2 -1
 8  2 -8
 4  3  8
 6  3 -9
 8  3  0
 4  4 -1
 6  4 -8
 8  4  8 

可以使用

绘制为plot_surface
ax = fig.gca(projection='3d')
ax.plot_surface(X.reshape(4,3), Y.reshape(4,3), Z.reshape(4,3))

任意数据

(a) 如果数据不在四边形网格上,可以在网格上插入数据。 matplotlib 本身提供了一种方法,使用matplotlib.mlab.griddata

import matplotlib.mlab
xi = np.linspace(4, 8, num=10)
yi = np.linspace(1, 4, num=10)
zi = matplotlib.mlab.griddata(X, Y, Z, xi, yi, interp='linear')
ax.plot_surface(xi, yi, zi)

(b) 最后,无需使用四边形网格即可完全绘制曲面。这可以使用plot_trisurf 来完成。

plt.plot_trisurf(X,Y,Z)

这个答案是 my answer for contour plots 的改编版本。

【讨论】:

  • 这些 (4, 8, 10) 值代表什么?
  • 为了reshape fun,出现错误:ValueError: total size of new array must be changed
  • linspace(4,8,10) 创建一个包含 4 到 8 之间的 10 个值的数组。Value 错误告诉您,整形必须采用相同数量的总元素,就像在示例中您有一个长度数组12 并将其重塑为 4 乘以 3 的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 2016-05-28
  • 2014-09-15
  • 1970-01-01
  • 2019-07-27
  • 2021-03-03
相关资源
最近更新 更多