用数据光标Matlab绘制四维图形显示变量 [英] 4D plot display variables with data cursor Matlab

查看:209
本文介绍了用数据光标Matlab绘制四维图形显示变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在我的情节中显示4个变量。

我想改变独立变量X,V来产生因变量Y和Z.
Y是X和V的函数。并且Z是Y和X的函数。



这可能更容易看到相关性:X,V,Y(X,V),Z(X,Y(X ,V))。

我已经使用surf函数来绘制X,Y,Z,但我也想知道V的值,目前我无法确定。

以下是一些测试数据来说明:

  X = linspace (1,5,5)
V = linspace(1,5,5)
捕获= []
对于j = 1:长度(V)
Y = X. * V(j)
Capture = [Capture; Y]
end
[X,V] = meshgrid(X,V);
Z = Capture。* X
surf(X,Y,Z)

如果我使用数据游标,我可以看到X,Y,Z的值,但我也想知道V的值。我知道我设置的方式是正确的,因为如果我制作两个图,说:

  surf(X,Y,Z)
surf(X,V,Z)

,然后使用数据光标在两个图的X和Z的同一点上显示V和Y的值是他们应该为那个点(X,Z)。



是否有无需显示X,Y,V和Z的值而无需生成两个单独的图?



感谢!

解决方案

使用颜色作为第四维是可能的味道)。

  surf(X,Y,Z,V); #%4th arg(V)映射到当前颜色映射

您可以改变色彩地图以适合您的口味。

  colorbar#%显示一个显示值颜色映射的颜色条图例



编辑:提问者希望看到未示数组中的数据,而不仅仅是一种颜色。这是自定义数据游标函数的工作。下面我使用纯粹的匿名函数实现了这个功能;在函数文件中执行它会稍微简单一些。

 #%步骤0:创建一个函数来索引一个数组。 .. 
#%由'get'返回的所有步骤
#%find(ismember ...位是它返回一个空矩阵...
#%如果索引(if / else语句不起作用...
#%在匿名函数中)
getel = @(x,i)x(find(ismember(1:numel(x) ,i)));

#%步骤1:创建一个自定义数据游标函数,它需要...
#%作为参数的附加矩阵
myfunc = @( obj,event_obj,data){...
['X:'num2str(getel(get(event_obj,'position'),1))],...
['Y:'num2str (getel(get(event_obj,'position'),2))],...
['Z:'num2str(getel(get(event_obj,'position'),3))],...
['V:'num2str(getel(data,get(event_obj,'dataindex')))];

#%第2步:获取datacursormode对象的句柄数字
dcm_obj = datacursormode(gcf);

#%Ste第3步:启用对象
set(dcm_obj,'enable','on')

#%第4步:将自定义函数设置为updatefcn,并给它额外的.. 。
#%data to be displayed
set(dcm_obj,'UpdateFcn',{myfunc,V})

现在工具提示应该显示额外的数据。请注意,如果您更改了图中的数据,您需要重复 Step 4 将新数据传递到函数中。


I am having trouble figuring out how display 4 variables in my plot.

I want to vary the independent variables X,V, to produce the dependent variables Y and Z. Y is a function of X AND V. And Z is a function of Y AND X.

This may be easier to see the dependencies: X, V, Y(X,V), Z(X,Y(X,V)).

I have used the surf function to plot X,Y,Z, but I also want to know the values of V, which I cannot currently ascertain.

Here is some test data to illustrate:

X = linspace(1,5,5)
V = linspace(1,5,5)
Capture = []
for j = 1:length(V)
Y = X.*V(j)
Capture = [Capture;Y]
end
[X,V] = meshgrid(X,V);
Z = Capture.*X
surf(X,Y,Z)

If I use the data cursor, I can see values of X,Y,Z, but I would also like to know the values of V. I know that the way I have it set up is correct because if I make two plots, say:

surf(X,Y,Z)
surf(X,V,Z) 

and then use the data cursor to go on the same point of X and Z for both graphs the values for V and Y are what they should be for that point (X,Z).

Is there anyway to show the values for X,Y,V and Z without having to generate two separate graphs?

Thanks!

解决方案

Using color as your 4th dimension is a possibility (whether it looks good to you is a matter of taste).

surf(X,Y,Z,V); #% 4th arg (V) is mapped onto the current colormap

You can change the colormap to suit your tastes.

colorbar #% displays a colorbar legend showing the value-color mapping

Edit: The questioner wants to see exactly the data in the not-shown array, rather than just a color. This is a job for custom data cursor function. Below I've implemented this using purely anonymous functions; doing it within a function file would be slightly more straightforward.

#% Step 0: create a function to index into an array...
#% returned by 'get' all in one step  
#% The find(ismember... bit is so it returns an empty matrix...
#% if the index is out of bounds (if/else statements don't work...
#% in anonymous functions)
getel = @(x,i) x(find(ismember(1:numel(x),i)));

#% Step 1: create a custom data cursor function that takes...
#% the additional matrix as a parameter
myfunc = @(obj,event_obj,data) {...
['X: ' num2str(getel(get(event_obj,'position'),1))],...
['Y: ' num2str(getel(get(event_obj,'position'),2))],...
['Z: ' num2str(getel(get(event_obj,'position'),3))],...
['V: ' num2str(getel(data,get(event_obj,'dataindex')))] };

#% Step 2: get a handle to the datacursormode object for the figure
dcm_obj = datacursormode(gcf);

#% Step 3: enable the object
set(dcm_obj,'enable','on')

#% Step 4: set the custom function as the updatefcn, and give it the extra...
#% data to be displayed
set(dcm_obj,'UpdateFcn',{myfunc,V})

Now the tooltip should display the extra data. Note that if you change the data in the plot, you'll need to repeat Step 4 to pass the new data into the function.

这篇关于用数据光标Matlab绘制四维图形显示变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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