简体   繁体   中英

2D linear interpolation: data and interpolated points

Consider this y(x) function:

在此处输入图片说明

where we can generate these scattered points in a file: dataset_1D.dat :

# x   y   
0   0
1   1
2   0
3   -9
4   -32

The following is a 1D interpolation code for these points:

  1. Load this scattered points

  2. Create a x_mesh

  3. Perform a 1D interpolation

Code:

import numpy as np
from scipy.interpolate import interp2d, interp1d, interpnd
import matplotlib.pyplot as plt


# Load the data:    
x, y  = np.loadtxt('./dataset_1D.dat', skiprows = 1).T

# Create the function Y_inter for interpolation:
Y_inter = interp1d(x,y)

# Create the x_mesh:    
x_mesh = np.linspace(0, 4, num=10)
print x_mesh

# We calculate the y-interpolated of this x_mesh :   
Y_interpolated = Y_inter(x_mesh)
print Y_interpolated

# plot:

plt.plot(x_mesh, Y_interpolated, "k+")
plt.plot(x, y, 'ro')
plt.legend(['Linear 1D interpolation', 'data'], loc='lower left',  prop={'size':12})
plt.xlim(-0.1, 4.2)
plt.grid()
plt.ylabel('y')
plt.xlabel('x')
plt.show()

This plots the following:

在此处输入图片说明

Now, consider this z(x,y) function:

在此处输入图片说明

where we can generate these scattered points in a file: dataset_2D.dat :

# x    y    z
0   0   0
1   1   0
2   2   -4
3   3   -18
4   4   -48

In this case we would have to perform a 2D interpolation:

import numpy as np
from scipy.interpolate import interp1d, interp2d, interpnd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load the data:
x, y, z  = np.loadtxt('./dataset_2D.dat', skiprows = 1).T

# Create the function Z_inter for interpolation:
Z_inter = interp2d(x, y, z)

# Create the x_mesh and y_mesh :
x_mesh = np.linspace(1.0, 4, num=10)
y_mesh = np.linspace(1.0, 4, num=10)
print x_mesh
print y_mesh

# We calculate the z-interpolated of this x_mesh and y_mesh :
Z_interpolated = Z_inter(x_mesh, y_mesh)
print Z_interpolated
print type(Z_interpolated)
print Z_interpolated.shape

# plot: 
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z, c='r', marker='o')
plt.legend(['data'], loc='lower left',  prop={'size':12})
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

This plots the following:

在此处输入图片说明

where the scattered data is shown again in red dots, to be consistent with the 2D plot.

  1. I do not know how to interpret the Z_interpolated result:

    According to the printing lines for the above code, Z_interpolated is a n-dimensional numpy array, of shape (10,10). In other words, a 2D matrix with 10 rows and 10 columns.

I would have expected an interpolated z[i] value for each value of x_mesh[i] and y_mesh[i] Why I do not receive this ?

  1. How could I plot also in the 3D plot the interpolated data (just like the black crosses in the 2D plot)?

Interpretation of Z_interpolated : your 1-D x_mesh and y_mesh defines a mesh on which to interpolate . Your 2-D interpolation return z is therefore a 2D array with shape (len(y), len(x)) which matches np.meshgrid(x_mesh, y_mesh) . As you can see, your z[i, i], instead of z[i], is the expected value for x_mesh[i] and y_mesh[i] . And it just has a lot more, all values on the mesh.

A potential plot to show all interpolated data:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp2d

# Your original function
x = y = np.arange(0, 5, 0.1)
xx, yy = np.meshgrid(x, y)
zz = 2 * (xx ** 2) - (xx ** 3) - (yy ** 2)

# Your scattered points
x = y = np.arange(0, 5)
z = [0, 0, -4, -18, -48]

# Your interpolation
Z_inter = interp2d(x, y, z)
x_mesh = y_mesh = np.linspace(1.0, 4, num=10)
Z_interpolated = Z_inter(x_mesh, y_mesh)

fig = plt.figure()
ax = fig.gca(projection='3d')
# Plot your original function
ax.plot_surface(xx, yy, zz, color='b', alpha=0.5)
# Plot your initial scattered points
ax.scatter(x, y, z, color='r', marker='o')
# Plot your interpolation data
X_real_mesh, Y_real_mesh = np.meshgrid(x_mesh, y_mesh)
ax.scatter(X_real_mesh, Y_real_mesh, Z_interpolated, color='g', marker='^')
plt.show()

在此处输入图片说明

You would need two steps of interpolation. The first interpolates between y data. And the second interpolates between z data. You then plot the x_mesh with the two interpolated arrays.

x_mesh = np.linspace(0, 4, num=16)

yinterp = np.interp(x_mesh, x, y)
zinterp = np.interp(x_mesh, x, z)

ax.scatter(x_mesh, yinterp, zinterp, c='k', marker='s')

In the complete example below I added some variation in y direction as well to make the solution more general.

u = u"""# x    y    z
0   0   0
1   3   0
2   9   -4
3   16   -18
4   32   -48"""

import io
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load the data:
x, y, z  = np.loadtxt(io.StringIO(u), skiprows = 1, unpack=True)

x_mesh = np.linspace(0, 4, num=16)

yinterp = np.interp(x_mesh, x, y)
zinterp = np.interp(x_mesh, x, z)

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x_mesh, yinterp, zinterp, c='k', marker='s')
ax.scatter(x, y, z, c='r', marker='o')
plt.legend(['data'], loc='lower left',  prop={'size':12})
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

在此处输入图片说明

For using scipy.interpolate.interp1d the solution is essentially the same:

u = u"""# x    y    z
0   0   0
1   3   0
2   9   -4
3   16   -18
4   32   -48"""

import io
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load the data:
x, y, z  = np.loadtxt(io.StringIO(u), skiprows = 1, unpack=True)

x_mesh = np.linspace(0, 4, num=16)

fy = interp1d(x, y, kind='cubic')
fz = interp1d(x, z, kind='cubic')

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x_mesh, fy(x_mesh), fz(x_mesh), c='k', marker='s')
ax.scatter(x, y, z, c='r', marker='o')
plt.legend(['data'], loc='lower left',  prop={'size':12})
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM