I want to ask two questions here. In short they are,
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.
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?
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
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.
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)
Creates,
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.
For the second question, doing,
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')
gives,
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.
See Question&Answers more detail:
os