Issue
I have a 3*3 table so my expected is to use interp2d
interpolating then predict a bigger table maybe 5*5 or 10*10 to get more results then show in plot_surface
- This is a simple 3*3 table for test and relationship:
x = np.array([1, 2,3]) #---X,Y,Z relationship------
y = np.array([0.05, 0.5,1]) #(1, 0.05, -1.0)(1, 0.5, -0.5)(1, 1.0, 2.0)
z = np.array([-1, -0.5,2,\ #(2, 0.05, -2.0)(2, 0.5, 1.5)(2, 1.0, 3.5)
-2, 1.5,3.5, #(3, 0.05, -1.5)(3, 0.5, 2.5)(3, 1.0, 5.0)
-1.5,2.5,5])
- To achieve this relationship then i set:
X,Y=np.meshgrid(x,y,indexing='ij')
Z=z.reshape(len(x),len(y))
- Interploting 5*5 tabble based on the current data
#interp2d Z value
f2 = interp2d(x,y,Z,kind='linear')
x_new=np.linspace(0.01,0.02,5)
y_new=np.linspace(0.002,0.004,5)
X_new,Y_new=np.meshgrid(x_new,y_new,indexing='ij')
z_new=f2(x_new,y_new)
Z_new=z_new.reshape(len(x_new),len(y_new))
print(z_new)
Now at this step i get the wrong number of interploted Z value,all the same and not expected
# [-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]]
So finlly the 3Dsurface become a flat picture
I am not sure why the script or function interp2d
wrong with it.
How can i fix the scipts?
This is my full script:
from scipy.interpolate import interp1d,interp2d,griddata
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x = np.array([1, 2,3])
y = np.array([0.05, 0.5,1])
z = np.array([-1, -0.5,2,\
-2, 1.5,3.5,
-1.5,2.5,5])
fig = plt.figure()
ax=Axes3D(fig)
ax = fig.add_subplot(projection='3d')
X,Y=np.meshgrid(x,y,indexing='ij')
Z=z.reshape(len(x),len(y))
#interp2d Z value
f2 = interp2d(x,y,Z,kind='linear')
x_new=np.linspace(0.01,0.02,5)
y_new=np.linspace(0.002,0.004,5)
X_new,Y_new=np.meshgrid(x_new,y_new,indexing='ij')
z_new=f2(x_new,y_new)
Z_new=z_new.reshape(len(x_new),len(y_new))
print(z_new) #---->not as expected [[-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]
# [-1. -1. -1. -1. -1.]]
#This is for check X,Y,Z value
def Check():
n,j=0,0
print("----X,Y,Z-----")
for i in zip(X.flat,Y.flat,Z.flat): #----X, Y, Z - ----
print(i, end=" ") #(1, 0.05, -1.0)(1, 0.5, -0.5)(1, 1.0, 2.0
n += 1 #(2, 0.05, -2.0)(2, 0.5, 1.5)(2, 1.0, 3.5)
if n % int(len(x))==0: #(3, 0.05, -1.5)(3, 0.5, 2.5)(3, 1.0, 5.0)
print()
print("----X_new,Y_new,Z_new-----")
for i in zip(X_new.flat,Y_new.flat,Z_new.flat):
print(i, end=" ")
j += 1
if j % int(len(x_new))==0:
print()
Check()
ax.plot_surface(X, Y, Z,linewidth=0,antialiased=True,cmap="cividis",rstride=1,cstride=1)
ax.plot_surface(X_new, Y_new, Z_new, linewidth=0, antialiased=True, cmap=cm.winter, rstride=1, cstride=1)
plt.show()```
Solution
You are extrapolating not interpolating. As per the documentation, the default functionality is to use the nearest value. Since all of your new values are closest to the (1,0.05) point, it uses -1 for all points.
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.