gpt4 book ai didi

python - 使用 Pyplot 绘制平滑曲面

转载 作者:行者123 更新时间:2023-11-28 20:04:49 35 4
gpt4 key购买 nike

我的问题几乎与此类似: smoothing surface plot from matrix

只是我的工具集是 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)

但是,由于这些值非常不稳定,所以看起来很糟糕。 Exampleplot - terribly edgy, ugly... not usable and stuff

我想把事情弄平,至少让顶点相连,或者看起来像那样。

我的数据是这样生成的:我有一个功能

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但我没有让它工作。

最佳答案

从您建议的链接中,示例 here可能最接近你想要的。您可以将示例与您的值一起使用,

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 可能会让它看起来更好,但我认为默认情况下是打开的。第一个情节看起来像这样,

enter image description here

像这样的平滑图,

enter image description here

数据中的低频噪声问题是很难定义足够精细的网格来解决。您可以使用 s 参数调整平滑级别到 interpolate.bisplrep 或者粗粒度/过滤您的数据以仅保留主要趋势(例如,如果您使用 scipy.ndimage.interpolation.zoom有规则的网格数据)。或者,考虑不同类型的图,例如 pcolormesh,因为数据本质上是二维的。

关于python - 使用 Pyplot 绘制平滑曲面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157650/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com