How do I set tooltip strings on scatter children?

8 views (last 30 days)
Hello,
I am attempting to programmatically set the TooltipString property for all of the points in my scatter plot and so far have not quite gotten there.
My intention is something like:
h = scatter(xPts,yPts,'b+');
allPts = get(h,'Children');
for i=1:length(allPts)
set(allPts{i},'TooltipString',pointNames(i));
end
... where the first get() would return the list of handles to point objects that could have individual tooltips set to their point names. I am apparently not getting the object hierarchy correct and/or am on the wrong track entirely.
Is an example of this reasonably documented?
Thanks, david
  1 Comment
David O
David O on 8 Jun 2011
OK, the end of this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/41100
has an interactive solution, but I would like to do the same within a tooltip callback.

Sign in to comment.

Accepted Answer

David O
David O on 9 Jun 2011
This solution is sufficient, though still using an interactive model (click button 1 to label the nearest point, button 2 to end):
xChildren = get(get(gca,'Children'),'XData');
yChildren = get(get(gca,'Children'),'YData');
xAll = xChildren{3}; % In my plot, there are 3 scatter groups plotted
yAll = yChildren{3}; % and I am interested in the elements from the 3rd.
btn = 1;
txtH = text(0,0,''); % set up dummy handle at first
maxX = max(xAll);
maxY = max(yAll);
XMARGIN = 0.05;
while btn == 1,
[xin,yin,btn]=ginput(1);
hold on;
% slow but not bad for our limited size data set
mindist = 100000000;
minItem = 0;
for item=1:length(xAll);
distSq = ((xAll(item)-xin)/maxX)^2 + ((yAll(item)-yin)/maxY)^2;
if distSq < mindist,
mindist = distSq;
minItem = item;
end
end
% if distSq < DIST_THRESH,
disp([biopsystrs{1,minItem} 'dist: ' num2str(distSq)]);
delete(txtH); % erase previous text
txtH = text(xAll(minItem)+XMARGIN,yAll(minItem),biopsystrs{1,minItem});
% else
% disp(['too far: ' num2str(distSq)]);
% end
end % end while button 1

More Answers (2)

Walter Roberson
Walter Roberson on 8 Jun 2011
scatter() does not create individual point objects.
I do not know if it is possible to set tooltipstring strings for the individual points. The only approach I can think of at the moment is to create a datacursor object and set the callback function for it to figure out which point is involved and display the appropriate string.

Matt Fig
Matt Fig on 8 Jun 2011
The children of the handle to h are patch objects, which have no tooltipstring. You could do something similar to what I did here, though it would take a more work because patches don't have a position property. So you would have to translate the xdata and ydata into positions relative to the figure....

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!