带有Pyplot的平滑表面图
我的问题几乎与此类似: 从矩阵平滑表面图
只是我的工具集是matplotlib和numpy(到目前为止)。
我已经成功生成了X,Y和Z网格以进行绘制
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='summer', rstride=1, cstride=1, alpa=None)
我想使事情变得平滑,至少使顶点连接或看起来像那样。
我的数据是这样生成的:我有一个函数
svOfMatrix(x, y)
它根据x生成矩阵,计算其y次幂,选择列和行的子集,并计算最大奇异值。因此,Z [x,y]是svOfMatrix(x,y)
由于此计算非常昂贵,因此我不想使x的步长太小,而Y必然要是整数。
此外,即使步长很小,也可能会有很多变化,我不想看到。所以我想以某种方式插值。我找到了
http://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/interpolate.html,
但是我没有用。
-
通过您建议的链接,此处的示例可能最接近您想要的示例。您可以将示例与值一起使用,
import numpy as np from scipy import interpolate import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d, Axes3D X, Y = np.mgrid[-1:1:20j, -1:1:20j] Z = (X+Y) * np.exp(-6.0*(X*X+Y*Y)) + np.random.rand(X.shape[0]) xnew, ynew = np.mgrid[-1:1:80j, -1:1:80j] tck = interpolate.bisplrep(X, Y, Z, s=0) znew = interpolate.bisplev(xnew[:,0], ynew[0,:], tck) fig = plt.figure(figsize=(12,12)) ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z, cmap='summer', rstride=1, cstride=1, alpha=None) plt.show() fig = plt.figure(figsize=(12,12)) ax = fig.gca(projection='3d') ax.plot_surface(xnew, ynew, znew, cmap='summer', rstride=1, cstride=1, alpha=None, antialiased=True) plt.show()
另外,
antialiased=True
可能会使它看起来更好,但我认为默认情况下处于启用状态。第一个情节看起来像这样,和这样的平滑图
数据中的低频噪声的问题在于,很难定义足够精细以解决问题的网格。您可以使用
s
参数interpolate.bisplrep
或粗粒度/过滤数据来调整平滑程度,以仅保留主要趋势(例如,scipy.ndimage.interpolation.zoom
如果您有规则的网格数据,则使用)。或者,考虑使用其他类型的图,例如pcolormesh,因为数据本质上是2D的。