如何在表面图上投影一条线? [英] How to project a line on a surfaceplot?
问题描述
我有一个根据存储在CSV文件中的点数据创建的曲面图。如果要在3D创建的曲面上投影一条线(浮在曲面上方)。用什么方法?
我尝试了
我已经生成了一个曲面,给定的分散点存储在CSV文件中。现在,我想将曲面(红线)上的线投影到曲面(如绿线)上。
让我们构建一个通用的MCVE,首先我们导入所需的软件包:
import numpy as np
from scipy import interpolate
as plt
从mpl_toolkits导入matplotlib.pyplot导入mplot3d
import matplotlib.tri as mtri
np.random.seed(123456)#修复随机种子
现在,我们生成表面 S
的3D点集合(注意它是不规则的网格):
NS = 100
Sx = np.random.uniform(low = -1 。,high = 1。,size =(NS,))
Sy = np.random.uniform(low = -1。,high = 1。,size =(NS,))
Sz = -(Sx ** 2 + Sy ** 2)+ 0.1 * np.random.normal(size =(NS,))
和参数曲线 P
:
NP = 100
t = np.linspace(-1,1,NP)
Px = t
Py = t ** 2-2- 0.5
Pz = t ** 3 + 1
解决问题的关键是
完整的3D结果显示如下:
axe = plt.axes(projection ='3d')
axe.plot_trisurf(tri,Sz,cmap ='jet',alpha = 0.5)
axe.plot(Px,Py, Pz)
axe.plot(Px,Py,PSz,线宽= 2,color ='黑色')
axe.scatter(Sx,Sy,Sz)
axe.view_init(elev = 25,azim = -45)
axe.view_init(elev = 75,azim = -45)
I Have a surface plot created from point data stored in a CSV file. If I want to project a line (which is floating above the surface) on the surface created in 3D. What is the method?
I have tried a code from the following post for projecting a line on xy-xz-yz plane.
I can see that it is projecting the endpoint of line on the xy-xz-yz plane.
If I want to project on the surface created with point data. I don't have an equation of the surface. I have created with point data available.
Here is a mockup image of what I'm trying to achieve:
I have generated a curved surface with given scattered points stored in a CSV file. Now I want to project the line on top of the surface(red line) to the surface(as a green line).
Lets build a generic MCVE, first we import required packages:
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.tri as mtri
np.random.seed(123456) # Fix the random seed
Now we generate a collection of 3D points for a surface S
(notice it is an irregular mesh):
NS = 100
Sx = np.random.uniform(low=-1., high=1., size=(NS,))
Sy = np.random.uniform(low=-1., high=1., size=(NS,))
Sz = -(Sx**2 + Sy**2) + 0.1*np.random.normal(size=(NS,))
And a parametric curve P
:
NP = 100
t = np.linspace(-1, 1, NP)
Px = t
Py = t**2 - 0.5
Pz = t**3 + 1
The key to solve your problem is LinearNDInterpolator
which performs a piecewise linear interpolation in N dimensions:
PSz = interpolate.LinearNDInterpolator(list(zip(Sx, Sy)), Sz)(list(zip(Px,Py)))
There is just the need to reshape data to fit the method signature from separate vectors to matrix of shape (Nsample,Ndims)
which can be translated to:
list(zip(Sx, Sy))
We can check the data from the top:
tri = mtri.Triangulation(Sx, Sy)
fig, axe = plt.subplots()
axe.plot(Sx, Sy, '+')
axe.plot(Px, Py)
axe.triplot(tri, linewidth=1, color='gray')
axe.set_aspect('equal')
axe.grid()
The complete 3D result is shown bellow:
axe = plt.axes(projection='3d')
axe.plot_trisurf(tri, Sz, cmap='jet', alpha=0.5)
axe.plot(Px, Py, Pz)
axe.plot(Px, Py, PSz, linewidth=2, color='black')
axe.scatter(Sx, Sy, Sz)
axe.view_init(elev=25, azim=-45)
axe.view_init(elev=75, azim=-45)
这篇关于如何在表面图上投影一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!