MATLAB:在表面图上绘制 [英] MATLAB: Drawing atop a surface plot

查看:306
本文介绍了MATLAB:在表面图上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MATLAB中的R ^ 2到R函数绘制为表面图,我从上面进行颜色映射和查看.

I'm plotting an R^2 to R function in MATLAB as a surface plot, which I colormap and view from above.

surf(X, Y, data);
colormap(jet);
colobar;
view(2);

它产生(带有一些附加代码)类似

It produces (with some additional code) something like

尽管从以下角度可以更好地观察到该函数的真实性质(出于理解该问题的目的):

though the true nature of the function (for the purpose of understanding this question) is better observed from an angle like:

我想在我的原始图上画一个圆(从上方看).像...

I want to plot a circle atop my original plot (seen from above). Something like...

但是,我似乎无法实现这一点,因为在平面图上绘制平面内元素会使它们显示在x-y轴上,这被我的平面图所覆盖.例如,调用

I can't seem to achieve this however, since plotting in-a-plane elements on plots makes them appear on the x-y axis, which is covered by my surface plot. For example, calling

circle_pos = [ +1 +1; -1 -1; -1 +1; +1 -1;]
circle_rad = 0.2 * ones(4,1);
viscircles(circle_pos, circle_rad);

从顶部查看时,我的表面图没有可见的圆圈.缩放和旋转显示这些圆被绘制在x-y平面上,因此从上方是不可见的.

after my surface plot results in no visible circles when viewed from the top. Zooming and rotating reveals these circles were plotted on the x-y plane, and so are invisible from above.

如何在表面图的顶部上绘制我的圆,以便可以从上方看到它们? 在表面上绘制text时也会出现类似的问题,但可以通过在基础函数z值正上方指定z位置值来解决.似乎没有任何方法可以指定这些图形元素的z位置.

How do I plot my circles on top of the surface plot, so that they are visible from above? A similar issue arises when plotting text atop the surface, but is remedied by specifying a z position value just above the underlying functions z value. There doesn't seem to be any way to specify the z position of these graphical elements.

推荐答案

可能没有 direct 方法来指定viscircles返回的对象的z位置,但是通常之后(大多数情况下),有一种方法可以修改任何图形对象的属性和位置.

There may not be a direct way to specify the z position of the objects returned by viscircles, but in general there is (most of the time) a way to modify properties and position of any graphic object afterwards.

如果您打算对图形对象进行修改,那么首先要做的就是始终检索其

If you plan to do modifications of a graphic object, the first thing to do is always to retrieve its handle. So in your case, you would have to call viscircles by specifying a return value (which will contain the handle you want).:

hg = viscircles(circle_pos, circle_rad);

我没有Image Processing Toolbox,所以我无法使用viscircles功能.但是,我从文档中了解到,返回的句柄是 hggroup . hggroup只是一个容器,其中包含一个或多个handles个更原始的图形对象.在这种情况下,hggroup包含4个lines的手柄(您的4个圆圈).

I do not have the Image Processing Toolbox so I do not have access to the viscircles function. However I read from the documentation that the handle returned is an hggroup. An hggroup is simply a container containing one or more handles of more primitive graphic objects. In this case the hggroup contains the handles of 4 lines (your 4 circles).

转换hggroup中所有对象的最简单方法是使用 hgtransform 对象.我们将定义一个 Translation 转换,然后hgtransform会将其应用于4个圆(hggroup的所有子代).

The easiest way to transform all the objects in an hggroup is to use a hgtransform object. We will define a Translation transformation and the hgtransform will apply it to the 4 circles (all the children of the hggroup).

要定义翻译,我们将使用 makehgtform 对象.

To define the translation, we will use a makehgtform object.

我们在这里:

ht = hgtransform ;      % create the transform object
set(hg,'Parent',ht) ;   % make it a "parent" of the hggroup

zc = max(max(Z)) ;  % Find by how much we want to translate the circles on the Z axis
Tz = makehgtform('translate',[0 0 zc]) ;   % create the TRANSLATION transform

set(ht,'Matrix',Tz)     % apply the transformation (translation) to the hggroup/hgtransform

完成后,您的4个圆圈现在应该在表面上.请注意,您可以为zc指定其他任何值(不仅限于表面的最大值).

Done, your 4 circles should now be on top of your surface. Note that you can specify any other values for zc (not only the max of the surface).

如果您不想依赖图像处理工具箱,或者根本不依赖它,那么在3D空间中自己创建圆是相对容易的.

In case you do not want to be reliant on the image processing toolbox, or if you do not have it at all, it is relatively easy to create circles in a 3D space by yourself.

这是一个功能,该功能将以类似于viscircles的方式创建圆,但是它还允许您为圆的中心位置指定一个可选的z坐标.

Here is a function which will create circles in a way comparable to viscircles but it also let you specify an optional z coordinate for the circle centre positions.

circles_3D.m的代码:

function hg = circles_3d( pos , rad , varargin )

% get current axes handle and hold state
ax = gca ;
holdState = get(ax,'NextPlot') ;    % save state to reinstate after function
set(ax,'NextPlot','add') ;          % equivalent of "hold off"

tt = linspace(0,2*pi) ;
hg = hggroup(ax) ;
for k = 1:numel(rad)

    c = pos(k,:) ;
    r = rad(k) ;
    x = c(1) + r.*cos(tt) ;
    y = c(2) + r.*sin(tt) ;
    z = zeros(size(x)) ;
    if numel(c)==3 ; z = z + c(3) ; end

    plot3(hg,x,y,z,varargin{:}) ;
end

set(ax,'NextPlot',holdState) ; % restore axes hold state

您现在可以代替viscircles来调用此函数.我使用varargin参数将任何line属性转移到创建的圆上(因此您可以指定ColorLineWidth和其他任何您喜欢的典型参数.

You can now call this function instead of viscircles. I used the varargin parameter to transfer any line property to the circles created (so you can specify the Color, LineWidth, and any other typical parameter you like.

为了举例说明,我需要重新创建一个与您可比的曲面,并在最大值周围分布4个零"极点:

For the sake of an example, I need to recreate a surface comparable to your, with 4x "zero" poles distributed around the maxima:

pc = 0.5 ;  % pole centers
pw = 0.05 ; % pole widths

% surface definition
[X,Y] = meshgrid(-5:.1:5);
R = sqrt(X.^2 + Y.^2) + eps ;
Z = sin(R)./R;
% zero surface values around the defined poles
[idxPoles] = find(abs(X)>=pc-pw & abs(X)<=pc+pw & abs(Y)>=pc-pw & abs(Y)<=pc+pw ) ;
Z(idxPoles)= 0 ;
% display
hs = surf(X,Y,Z) ; shading interp

哪个生产:

现在,您只需使用circles_3D函数即可获得您的圈子:

Now you can simply get your circles with the circles_3D function:

zc = max(max(Z)) ;
circle_pos = [ pc pc zc ; -pc -pc zc ; -pc +pc zc ; +pc -pc zc ] ;
circle_rad = 0.2 * ones(4,1);
h = circles_3d( circle_pos , circle_rad , 'Color','r','LineWidth',2) ;

并获得:

请注意,我做了此功能,因此它还返回了包含您的线(圆)的hggroup对象.因此,如果您想稍后移动,请应用与答案第一部分相同的技巧.

Note that I made this function so it also return an hggroup object containing your lines (circles). So if you want to move them later, apply the same trick than in the first part of the answer.

这篇关于MATLAB:在表面图上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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