给出“ ValueError”的矩形网格上的曲面图

问题描述

我正在尝试在矩形网格上绘制图。这是我的代码

Z = np.reshape(FV,(N_x,N_y))
x = np.linspace(0,1,N_x)
y = np.linspace(0,N_y)

X,Y = np.meshgrid(x,y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='viridis',edgecolor='none')

我得到了错误ValueError: shape mismatch: objects cannot be broadcast to a single shape

这仅在N_x == N_y时有效,如何使它在N_x!= N_y时有效?

解决方法

这恰好是实现numpy.meshgrid的一种方式-检查索引上的注释。

y 变量沿行随默认索引'xy'变化(显然, x 变量沿列随行!)-从几何角度考虑-水平是 x ,垂直是 y 。因此,您有两种可能的解决方案:

  • Z = np.reshape(FV,(N_x,N_y))更改为Z = np.reshape(FV,(N_y,N_x))。也许有点不直观,但效果很好。
  • 使用带有indexing = 'ij'np.meshgrid的关键字参数public TreeNode pruneTree(TreeNode root) { root = cleanTree(root); return root; } public TreeNode cleanTree(TreeNode node) { if (node == null) { return node; } node.left = cleanTree(root.left); node.right = cleanTree(root.right); if (node.val == 0) { if (node.left == null) { return node.right; } if (node.right == null) { return node.left; } // Bad luck ... should delete node. } return node; } 来更改随行变化的 x 变量-在使用矩阵时,我们已经很想过这一点。