如何创建表面图的切片以创建线? (Matlab) [英] How can I create a slice of a surface plot to create a line? (Matlab)

查看:127
本文介绍了如何创建表面图的切片以创建线? (Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一些函数z = f(x,y),我感兴趣的是沿着x,y,z中的任意切割平面创建(1D)线图.如何在Matlab中做到这一点?例如,Slice提供了更高维度的版本(密度数据的颜色图),但这不是我想要的.

Given some function z = f(x,y), I'm interested in creating a (1D) line plot along an arbitrary cutting plane in x,y,z. How do I do this in Matlab? Slice, for example, provides a higher dimensional version (colormap of density data) but this is not what I'm looking for.

例如:

z = peaks(50);
surf(z);
%->plot z along some defined plane in x,y,z...

例如,以前曾有人问过这个问题. 此处 ,但这是为将3D数据还原为2D数据而给出的答案,并且在谷歌搜索上没有明显的答案.谢谢.

This has been asked before, e.g. here, but this is the answer given is for reducing 3D data to 2D data, and there is no obvious answer on googling. Thanks.

推荐答案

如果要切片的平面的法线矢量始终位于xy平面中,则可以沿x方向在表面上插值数据,例如,在切片线上的y坐标将平面定义为从点(0,15)到点(50,35)

If the normal vector of the plane you want to slice your surface will always lay in the xy plane, then you can interpolate the data over your surface along the x,y coordinates that are in the slicing line, for example, let the plane be defined as going from the point (0,15) to the point (50,35)

% Create Data
z=peaks(50);

% Create x,y coordinates of the data
[x,y]=meshgrid(1:50);

% Plot Data and the slicing plane 
surf(z);
hold on
patch([0,0,50,50],[15,15,35,35],[10,-10,-10,10],'w','FaceAlpha',0.7);

% Plot an arbitrary origin axis for the slicing plane, this will be relevant later
plot3([0,0],[15,15],[-10,10],'r','linewidth',3);

由于它是一个平面,所以用linspace相对于切片平面获取x,y坐标相对容易,我将得到100个点,然后将这100个点插值到原始数据中.

Since it is a plane, is relatively easy to obtain the x,y coordinates alogn the slicing plane with linspace, I'll get 100 points, and then interpolate those 100 points into the original data.

% Create x and y over the slicing plane
xq=linspace(0,50,100);
yq=linspace(15,35,100);

% Interpolate over the surface
zq=interp2(x,y,z,xq,yq); 

现在我们有了z的值,我们需要针对它们绘制的对象相对应,这就是您需要为拼接平面定义任意原点轴的原因,为方便起见,我在(0,15)处定义了mine轴,然后计算每个x,y对到该轴的距离,然后可以针对该距离绘制获得的z.

Now that we have the values of z, we need against what to plot them against, that's where you need to define an arbitrary origin axis for your splicing plane, I defined mine at (0,15) for convenience sake, then calculate the distance of every x,y pair to this axis, and then we can plot the obtained z against this distance.

dq=sqrt((xq-0).^2 + (yq-15).^2);

plot(dq,zq)

axis([min(dq),max(dq),-10,10]) % to mantain a good perspective

这篇关于如何创建表面图的切片以创建线? (Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆