python - 使用 matplotlib 的 3D 表面不正确,但等高线图正确
问题描述
我正在尝试使用 matplotlib 为一堆数学函数绘制 3D 曲面。这些函数被定义为以任意长度的一维 numpy 数组作为输入。
当绘制为等高线图时,该图看起来是正确的。. 但是,3D 曲面图显示了一个已被压扁到一条线上的曲面。我使用相同的值进行绘图,所以它们应该是相同的,但这不是我得到的,我对此感到非常困惑
请在下面查看我的代码:
from mpl_toolkits.mplot3d import Axes3D
# build the meshgrid
x = np.linspace(bounds[0][0],bounds[0][1])
y = np.linspace(bounds[1][0], bounds[1][1])
xv, yv = np.meshgrid(x, y)
# populate z
z = np.zeros_like(xv)
for row_idx in range(xv.shape[0]):
for col_idx in range(xv.shape[1]):
z[row_idx][col_idx] = function(np.array([xv[row_idx][col_idx], yv[row_idx][col_idx]]))
# plot 3D surface
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')
# plot a contour
ax.contourf(x, y, z, cmap='viridis')
plt.show()
# function that I'm plotting -> bowl shaped for 2D x
def function(x):
return sum(x**2)
解决方案
推荐阅读
- r - R - 更新列表中的元素(循环中)
- javafx - ImageView 中的 JavaFx Gif 停止
- android - Unity Android 游戏崩溃 android.os.TransactionTooLargeException
- python - 在 Selenium Python 中只能下载 4 MB 的文件 | 铬驱动程序
- sql - 使用参数将值插入表中
- java - 泛型的概念真的提供类型安全吗?
- swiftui - 向 SwiftUI navigationBarTitle 添加点击手势?
- r - 使用 coxph 模型预测每个因素的事件概率
- python - 如何向我的 API 发出正确的 PUT REQUEST?JSONDecodeError:无效的控制字符在:第 1 行第 40 列
- ruby - Inspec 命令不打印服务器的内存