[英]How to interpolate a 2D surface using Scipy for Matplotlib rendering?
Given a 2D surface with few points that is displayed using Matplotlib:给定一个使用 Matplotlib 显示的带有少量点的 2D 表面:
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
x = np.arange(0, 4, 1)
y = np.arange(0, 4, 1)
x, y = np.meshgrid(x, y)
z = np.array([
[0, 1, 1, 0],
[1, 2, 2, 1],
[1, 3, 2, 1],
[0, 1, 1, 0]])
surf = ax.plot_surface(x, y, z, cmap=cm.cool)
plt.show()
How can I interpolate the data to make the surface smoother?如何插入数据以使表面更平滑? Ideally, I would want to use a spline interpolation (similar to this example ).理想情况下,我想使用样条插值(类似于这个例子)。 Consequently, I tried to use interpolate.bisplrep
from Scipy but got various TypeError: len(x)==len(y)==len(z) must hold
errors.因此,我尝试使用Scipy 中的interpolate.bisplrep
但得到了各种TypeError: len(x)==len(y)==len(z) must hold
errors。 How can I prepare the data?我该如何准备数据?
interpolate.bisplrep
requires that the x, y
edges are arranged using a meshgrid
: interpolate.bisplrep
要求使用meshgrid
排列x, y
边缘:
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from scipy import interpolate
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
x = np.arange(0, 4, 1)
y = np.arange(0, 4, 1)
x, y = np.meshgrid(x, y)
z = np.array([
[0, 1, 1, 0],
[1, 2, 2, 1],
[1, 3, 2, 1],
[0, 1, 1, 0]])
# new grid is 40x40
xnew = np.linspace(0, 3, num=40)
ynew = np.linspace(0, 3, num=40)
tck = interpolate.bisplrep(x, y, z, s=0)
znew = interpolate.bisplev(xnew, ynew, tck)
xnew, ynew = np.meshgrid(xnew, ynew)
surf = ax.plot_surface(xnew, ynew, znew, cmap=cm.cool)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.