我是在 Python 中使用 matplotlib 模块的新手,尤其是绘制 3D 曲面。我已经生成了一个数据矩阵,其中数据对应于
import numpy as np
z = np.linspace(0,3*np.pi,301)
x = np.linspace(0,2*np.pi,201)
在函数sin(z)sin(x)中,也就是说
Y = np.sin(z).reshape((301,1))*np.sin(x).reshape((1,201))
我设法使用代码绘制了这个表面
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111, projection='3d')
mycmap = plt.get_cmap('gist_earth')
ax1.set_title('gist_earth color map')
surf1 = ax1.plot_surface(Z, X, Y.T, cmap=mycmap, rstride=2, cstride=2)
ax1.view_init(45, 45)
fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=5)
结果如下:
然后,我想做同样的事情,但在数据矩阵中添加一个随机误差项。为此,新矩阵将定义为
Y = np.sin(zsam).reshape((301,1))*np.sin(xsam).reshape((1,201)) + np.random.normal(0,0.25,301*201).reshape((301,201))
然而,当我尝试绘制这个矩阵时,我得到了这个:
如何获得第一种情况下的光滑表面?任何帮助将不胜感激。