简体   繁体   中英

Python - How to get z value from given x, y after surface discrete 3D points (x,y,z)

I am newbie at Python. I am using a block code to draw the surface from discrete 3D points as a block code below:

#!/usr/bin/python3

import sys
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy import array, newaxis

DATA = array([
    [-0.807237702464, 0.904373229492, 111.428744443],
    [-0.802470821517, 0.832159465335, 98.572957317],
    [-0.801052795982, 0.744231916692, 86.485869328],
    [-0.802505546206, 0.642324228721, 75.279804677],
    [-0.804158144115, 0.52882485495, 65.112895758],
    [-0.806418040943, 0.405733109371, 56.1627277595],
    [-0.808515314192, 0.275100227689, 48.508994388],
    [-0.809879521648, 0.139140394575, 42.1027499025],
    [-0.810645106092, -7.48279012695e-06, 36.8668106345],
    [-0.810676720161, -0.139773175337, 32.714580273],
    [-0.811308686707, -0.277276065449, 29.5977405865],
    [-0.812331692291, -0.40975978382, 27.6210856615],
    [-0.816075037319, -0.535615685086, 27.2420699235],
    [-0.823691366944, -0.654350489595, 29.1823292975],
    [-0.836688691603, -0.765630198427, 34.2275056775],
    [-0.854984518665, -0.86845932028, 43.029581434],
    [-0.879261949054, -0.961799684483, 55.9594146815],
    [-0.740499820944, 0.901631050387, 97.0261463995],
    [-0.735011699497, 0.82881933383, 84.971061395],
    [-0.733021568161, 0.740454485354, 73.733621269],
    [-0.732821755233, 0.638770044767, 63.3815970475],
    [-0.733876941678, 0.525818698874, 54.0655910105],
    [-0.735055978521, 0.403303715698, 45.90859502],
    [-0.736448900325, 0.273425879041, 38.935709456],
    [-0.737556181137, 0.13826504904, 33.096106049],
    [-0.738278724065, -9.73058423274e-06, 28.359664343],
    [-0.738507612286, -0.138781586244, 24.627237837],
    [-0.738539663773, -0.275090412979, 21.857410904],
    [-0.739099040189, -0.406068448513, 20.1110519655],
    [-0.741152200369, -0.529726022182, 19.7019157715],
])

Xs = DATA[:,0]
Ys = DATA[:,1]
Zs = DATA[:,2]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)

ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))

fig.tight_layout()
plt.show()

Now I want to get Z value from any X and Y like z = f(x,y) but I don't know how can i do it. Anyone have solution to help me.

Thank you so much.

If you want to to turn this array of points into a continuous function with input x,y and output z, you need to describe it with a model. A model allows for interpolation of points.

I assume this array of points describes a surface plot, but plotting it is unnecessary.

A surface plot looks like this (I can only provide the link): surface plot

Each quadrilateral in the surface plot is defined by 4 points and represents part of a plane. So, a function to describe your array would be piece wise of all the different planes which make up your surface plot.

A plane requires 3 points to define it, so create a function to go through the array selecting 3 points in order, finding the equation of the plane and then adding it to an array of equations.

Code to find equation given 3 points:

def equation_plane(x1, y1, z1, x2, y2, z2, x3, y3, z3):    
    a1 = x2 - x1 
    b1 = y2 - y1 
    c1 = z2 - z1 
    a2 = x3 - x1 
    b2 = y3 - y1 
    c2 = z3 - z1 
    a = b1 * c2 - b2 * c1 
    b = a2 * c1 - a1 * c2 
    c = a1 * b2 - b1 * a2 
    d = (- a * x1 - b * y1 - c * z1) 
    print "equation of plane is ", 
    print a, "x +", 
    print b, "y +", 
    print c, "z +", 
    print d, "= 0."

Then for a given x and y find which plane contains these x and y points (use code similar to nils werners answer), get the corresponding equation and use that function to get the z coordinate.

This is a simple numpy array comparison and indexing problem. For a given pair of x, y

xy = numpy.array([-0.802470821517, 0.832159465335])

we can compare all rows to this at once, using

numpy.isclose(DATA[:, :2], xy)
# array([[False, False],
#        [ True,  True],
#        [False, False],
#        ....
#        [False, False],
#        [False, False]])

This will give us two boolean values per row, one for x and one for y . We are only interested in rows where both are True , so

numpy.all(numpy.isclose(DATA[:, :2], xy), axis=1)
# array([False,  True, False, ..., False, False, False, False])

The result we can then use to index DATA again, and extract z like

DATA[numpy.all(numpy.isclose(DATA[:, :2], xy), axis=1), -1]
# array([98.57295732])

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