MATLAB散布的工具提示显示了其他类型的数据以及带有箭头的连接点 [英] MATLAB scatter with tooltip showing other kinds of data and connecting points with arrowhead

查看:79
本文介绍了MATLAB散布的工具提示显示了其他类型的数据以及带有箭头的连接点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里问两个问题.简而言之,它们是

I want to ask two questions here. In short they are,

  1. 在MATLAB中的散点图中,如何使用工具提示单击一个点,而不是获取x,y数据,而是获取与x,y点关联的其他某种数据?现在,我有了使用gscatter和来自file-exchange的文件的解决方法(请参见下文).但是对于大数据集,它将变得凌乱.

  1. In a scatter plot in MATLAB how can I click a point using the tooltip and get not the x,y data but some other sort of data associated with the x,y point? Right now I have a workaround using gscatter and a file from file-exchange (see below). But it will get messy for large data sets.

如何将两个点之间的线上的箭头连接起来?例如,在MATLAB制作的rlocus图中,有一个漂亮的小箭头.在MATLAB中是否有本机方法可用于任意图?

How do I connect two points with an arrowhead on the line between them? For example in the rlocus plots MATLAB makes, there is a nifty little arrowhead. Is there a native way to do this in MATLAB for arbitrary plots?

考虑MATLAB中的数据集

Consider the data set in MATLAB

clearvars
LionNames={'Tyrion','Jamie','Cersei'};
Data = rand(3,2,2);
LionsDay1=struct('Names',{},'Data',[]);
LionsDay2=struct('Names',{},'Data',[]);

for i =1:numel(LionNames)
    LionsDay1(i).Names=LionNames{i};
    LionsDay1(i).Data=Data(i,:,1);
    LionsDay2(i).Names=LionNames{i};
    LionsDay2(i).Data=Data(i,:,2);
end

WolfNames = {'Robert','Arya','Sansa','Jon'};
Data = rand(4,2,2);
WolvesDay1=struct('Names',{},'Data',[]);
WolvesDay2=struct('Names',{},'Data',[]);

for i =1:numel(WolfNames)
    WolvesDay1(i).Names=WolfNames{i};
    WolvesDay1(i).Data=Data(i,:,1);
    WolvesDay2(i).Names=WolfNames{i};
    WolvesDay2(i).Data=Data(i,:,2);
end

这里每个组的数据是x和y数据.出于这个问题或示例的目的,上面的数据结构并不是那么重要,但是我已经做到了,以便读者对全局有所了解.

Here the data for each group is x and y data. For the purposes of this question or example, the data structure above is not all that important, but I have made it so the reader gets a feel for the big picture.

因此,使用MATLAB文件交换中的文件 ,我可以进行分散绘制并命名每个点及其类.例如

So using a file from MATLAB file exchange I am able to scatter plot and name each point as well as it's class. For example,

lionsData=reshape([LionsDay1(:).Data],2,3);
wolvesData=reshape([WolvesDay1(:).Data],2,4);
xData=[lionsData(1,:) wolvesData(1,:)];
yData=[lionsData(2,:) wolvesData(2,:)];
group=repmat({'Lions'},[1,3]);
group= [group repmat({'Wolves'},[1,4])];
gscatter(xData',yData',group');

Names=[LionNames WolfNames];
labelpoints(xData,yData,Names)

创建,

但是,正如您可以想象的那样,这对于大数据集(> 50个数据点)会变得混乱.或者这些点之间的距离很近,因此是第一个问题.单击一个点以显示名称会更好.

But as you can imagine, this would get messy for large data sets (>50 data points); or if the points were very close together, hence the first question. Clicking on a point to reveal the name would be much better.

第二个问题,在做

day1Lions=reshape([LionsDay1(:).Data],2,3);
day2Lions=reshape([LionsDay2(:).Data],2,3);

for k = 1 : size(day1Lions, 2)
    plot([day1Lions(1,k), day2Lions(1,k)], [day1Lions(2,k), day2Lions(2,k)],'s-');
    hold on
end
legend('Tyrion','Jamie','Cersei')

给予

所以从某种意义上说,我们可以看到第一天和第二天之间两点的变化量但是现在我不知道哪一天是第一天和第二天.将箭头从第1天数据点指向第2天数据点.当然,如果上面的悬停/工具提示问题具有足够灵活的答案,那么也可能会解决此问题.

So in a sense we can see how much the two points have changed between day 1 and day 2 but now I don't know which is day 1 and day 2. It would be nice to put an arrow going from the day 1 data point to day 2 data point. Of course if the hover/tooltip question above has a flexible enough answer, that might fix this problem too.

当然最后,我们还会在第一天和第二天混入狮子和狼,但是回答这两个简单的问题也有可能在合并后的情节中回答问题.

Of course in the end, we would also have lions and wolves mixed in with day 1 and day 2, but having these two simple questions answered would likely answer issues when doing the combined plot as well.

推荐答案

答案1

一种解决方案是您为data-tooltip定义自己的回调函数.为此,您首先需要在图中保存Names.我们可以使用UserData属性:

Answer 1

One solution is you define your own callback function for the data-tooltip. To do that, you first need to save Names within the figure. We can use UserData property for that:

% modify the end of your code to:
gsh = gscatter(xData',yData',group');
Names = [LionNames WolfNames];
set(gsh,{'UserData'},{Names});

接下来,我们创建以下回调函数(我使用了Matlab的默认格式并对其进行了编辑),并将其保存在新的m文件中:

Next, we create the following callback function (I took Matlab's defualt and edit it), and save it in new m-file:

function output_txt = tooltip_callback(obj,event_obj)
% Display the position of the data cursor
% obj          Currently not used (empty)
% event_obj    Handle to event object
% output_txt   Data cursor text string (string or cell array of strings).

pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)],...
    event_obj.Target.UserData{event_obj.Target.XData==pos(1)}}; % <- this line is the only change
% If there is a Z-coordinate in the position, display it as well
if length(pos) > 2
    output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end

现在,我们单击图中的工具提示之一,然后选择选择文本更新功能:

Now we click on one of the tooltips in the figure and choose Select Text Update Function:

,然后从浏览器中选择保存的回调函数.

and from the browser we pick the callback function we saved.

结果:

以同样的方式,您可以根据需要在工具提示中添加日期,或使用我对第二季度的回答...

In the same manner, you can add the days to the tooltip if you want, or use my answer to Q2...

在这里,您可以使用 annotations

Here is how you can use annotations to do that:

ax = axes;  % create the axis
% plot all lines (no need for the loop) so we can put the legend after:
p = plot(day1Lions,day2Lions,'-');
legend('Tyrion','Jamie','Cersei')
% get the lines colors:
col = cell2mat(get(p,'Color'));
% loop through the arrows:
for k = 1:size(day1Lions, 2)
    % get the data coordinates:
    x = day1Lions(:,k);
    y = day2Lions(:,k);
    pos = ax.Position;
    % convert them to normalized coordinates:    
    %  white area * ((value - axis min)  / axis length)  + gray area
    normx = pos(3)*((x-ax.XLim(1))./range(ax.XLim))+ pos(1);
    normy = pos(4)*((y-ax.YLim(1))./range(ax.YLim))+ pos(2);
    % plot the arrow
    annotation('arrow',normx,normy,'Color',col(k,:))
end

结果:

您还可以使用以下方法将原始行设置为不可见:

you could also set the original lines invisable, with:

set(p,{'Visible'},{'off'})

但是它将使图例文本变为灰色,并且它们仍然完全被箭头覆盖.

but it will turn the legend text gray, and they are totally covered by the arrows anyway.

这篇关于MATLAB散布的工具提示显示了其他类型的数据以及带有箭头的连接点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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