![](/img/trans.png)
[英]Python: How to interpolate errors using scipy interpolate.interp1d
[英]How to interpolate a 2D surface using Scipy for Matplotlib rendering?
給定一個使用 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()
如何插入數據以使表面更平滑? 理想情況下,我想使用樣條插值(類似於這個例子)。 因此,我嘗試使用Scipy 中的interpolate.bisplrep
但得到了各種TypeError: len(x)==len(y)==len(z) must hold
errors。 我該如何准備數據?
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.