我在Python(np
)中有一個numpy
對象,我正在其中繪製pyplot
中的圖。 這是一個含有x
,y
和z
的3D圖形,其中包含要繪製圖形的值。 下面是我的代碼(從here):查找對應於3D numpy對象中給定z值的x和y值
data = np.c_[x,y,z]
# regular grid covering the domain of the data
mn = np.min(data, axis=0) - 0.009
mx = np.max(data, axis=0) + 0.009
X,Y = np.meshgrid(np.linspace(mn[0], mx[0]), np.linspace(mn[1], mx[1]))
XX = X.flatten()
YY = Y.flatten()
A = np.c_[np.ones(data.shape[0]), data[:, :2], np.prod(data[:, :2], axis=1), data[:, :2] ** 2]
C, _, _, _ = scipy.linalg.lstsq(A, data[:, 2])
# evaluate it on a grid
Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX * YY, XX ** 2, YY ** 2], C).reshape(X.shape)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)
ax.scatter(data[:, 0], data[:, 1], data[:, 2], c='r', s=50)
plt.xlabel('X')
plt.ylabel('Y')
ax.set_zlabel('Z')
ax.axis('equal')
ax.axis('tight')
plt.show()
所以在這裏,我想要得到的x
和y
,其中z
值最接近-30
值。我如何獲得x
和y
的值。
如果我理解你的問題是正確的,這可能是有用的http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array –