答案1
一种解决方案是为data-tooltip定义自己的回调函数.要做到这一点,首先需要在图中保存名称.我们可以使用UserData属性:
% modify the end of your code to:
gsh = gscatter(xData',yData',group');
Names = [LionNames WolfNames];
set(gsh,{'UserData'},{Names});
接下来,我们创建以下回调函数(我使用了Matlab的defualt并对其进行编辑),并将其保存在新的m文件中:
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)}}; %
% 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
现在我们点击图中的一个工具提示,然后选择Select Text Update Function:
从浏览器中我们选择我们保存的回调函数.
结果:
以同样的方式,您可以根据需要将日期添加到工具提示中,或者使用我对Q2的回答…
答案2
以下是如何使用annotations执行此操作的方法:
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
结果:
您还可以设置原始行不可用,具有:
set(p,{'Visible'},{'off'})
但它会将传说文字变成灰色,无论如何它们完全被箭头所覆盖.