我有x和y数组来表示网格数据的行和列,其值在z 2d数组中。
我已经使用interpolate.interp2d绘制了一个表面图。
现在,我能够获得给定x,y值的z值,但我希望能够从给定z值获得x,y值。
码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy import interpolate
x = [0, 0.01, 0.02]
y = [0, 5e-05, 0.0001]
print(np.meshgrid(x, y))
xx, yy = np.meshgrid(x, y)
z = [[ 1. , 0.99990035, 0.99981654],
[ 0.90849264, 0.85447203, 0.81382534],
[ 0.82880434 , 0.76083003 , 0.71961281]]
f = interpolate.interp2d(x, y, z, kind='linear')
xnew = np.arange(x[0],x[2], 1e-6)
ynew = np.arange(y[0],y[2], 1e-6)
znew = f(xnew, ynew)
print(f(0.003,0.0006))
xnew,ynew=np.meshgrid(xnew,ynew)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(xnew,ynew, znew,
cmap=cm.get_cmap('coolwarm'))
plt.show()
最佳答案
无论如何绘制表面,您都可以沿着特定的z
值添加等高线图。为了从给定的z值获得x,y值,ob可以使用轮廓线的数据。
c = ax.contour(xnew,ynew,znew, [f(0.003,0.0006)])
line = c.allsegs[0][0]
print(line)
生成的
line
是一个二维数组,第一列的x值和第二列的y值。import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy import interpolate
x = [0, 0.01, 0.02]
y = [0, 5e-05, 0.0001]
xx, yy = np.meshgrid(x, y)
z = [[ 1. , 0.99990035, 0.99981654],
[ 0.90849264, 0.85447203, 0.81382534],
[ 0.82880434 , 0.76083003 , 0.71961281]]
f = interpolate.interp2d(x, y, z, kind='linear')
xnew = np.arange(x[0],x[2], 1e-6)
ynew = np.arange(y[0],y[2], 1e-6)
znew = f(xnew, ynew)
print(f(0.003,0.0006))
xnew,ynew=np.meshgrid(xnew,ynew)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(xnew,ynew, znew,
cmap=cm.get_cmap('coolwarm'))
c = ax.contour(xnew,ynew,znew, [f(0.003,0.0006)])
line = c.allsegs[0][0]
print(line)
ax.view_init(elev=13, azim=18)
plt.show()
关于python - 使用interpolate.interp2d绘制表面后如何从给定的Z值中获取X,Y值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49109466/