卓尔高考网

如何在使用interpolate.interp2d绘制曲面后从给定的Z值中获取X,Y值

篇首语:本文由小编为大家整理,主要介绍了如何在使用interpolate.interp2d绘制曲面后从给定的Z值中获取X,Y值相关的知识,希望对你有一定的参考价值。

我有x和y数组来表示网格数据的行和列,它的值在z 2d数组中。我使用interpolate.interp2d绘制表面图。现在,我能够获得给定x,y值的z值,但我希望能够从给定的z值获得x,y值。

码:

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib import cmfrom mpl_toolkits.mplot3d import Axes3Dfrom scipy import interpolatex = [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 npimport matplotlib.pyplot as pltfrom matplotlib import cmfrom mpl_toolkits.mplot3d import Axes3Dfrom scipy import interpolatex = [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()

以上是关于如何在使用interpolate.interp2d绘制曲面后从给定的Z值中获取X,Y值的主要内容,如果未能解决你的问题,请参考以下文章

您可能还会对下面的文章感兴趣: