的matplotlib形状不匹配错误与plot_surface

时间:2018-10-14 08:53:53

标签: python-3.x matplotlib graph 3d

因此,我有一个称为calculateY的函数,当给定两个点时,该函数返回高度。我想用它来计算要绘制的3d图形。

我对从heightMin到heightMax和radiusMin到radiusMax的点进行采样,并将每个点用作x,y,然后计算Y为z。

代码如下:

#Processing the data

zs = np.zeros((heightMax - heightMin, radiusMax - radiusMin))

for height in range(heightMin, heightMax):
  for radius in range(radiusMin, radiusMax):
    xs.append(radius)
    ys.append(height)
    zs[height- heightMin, radius- radiusMin] = calculateY(height, radius)

xs,ys = np.meshgrid(xs,ys)

#Initializing the graph
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

#Plot the graph
ax.plot_surface(xs, ys, zs)

plt.show()

ax.plot_surface(xs, ys, zs)引发错误“ ValueError:形状不匹配:对象无法广播为单个形状

我不知道该怎么做。

谢谢您的帮助。

编辑

现在可以正常工作-使用np.arrange()制作数组

#Processing the data

zs = np.zeros((heightMax - heightMin, radiusMax - radiusMin))

xs = np.arange(radiusMin, radiusMax, 1)
ys = np.arange(heightMin, heightMax,1)

xs,ys = np.meshgrid(xs,ys)

for height in range(heightMin, heightMax):
  for radius in range(radiusMin, radiusMax):
    zs[height- heightMin, radius- radiusMin] = calculateY(height, radius)
#Initializing the graph
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# ax = fig.gca(projection = '3d')

#Plot the graph
ax.plot_wireframe(xs, ys, zs)

plt.show()

不知道为什么

0 个答案:

没有答案