Incorrect datatip location on interpolated data
Show older comments
I am trying to automatically add datatips to my data, but have found some strange behaviour that I do not know how to solve.
I have reduced it to the following example:
figure;
hPlot = plot(1:10, 10:-1:1);
x = 1.58;
y = interp1(hPlot.XData, hPlot.YData, x); % Get y-coordinate at given x.
hDataTip = datatip(hPlot, x, y, 'SnapToDataVertex', 'off'); % Create the datatip
fprintf('Desired: x = %.2f, y = %.2f\nRealized: x = %.2f, y = %.2f\n', x, y, hDataTip.X, hDataTip.Y);
Which gives the following output:
Desired: x = 1.58, y = 9.42
Realized: x = 2.58, y = 8.42
Any x-coordinate that would round up to the next integer gives this strange result.
The problem can also be clearly seen when animating:
figure; hold on;
hPlot = plot(1:10, 10:-1:1);
hMarker = scatter(1,1, 'ro');
hDataTip = datatip(hPlot, 'SnapToDataVertex', 'off');
for(x = 1:0.04:5)
y = interp1(hPlot.XData, hPlot.YData, x); % Get y-coordinate at given x.
hDataTip.X = x;
hDataTip.Y = y;
hMarker.XData = x;
hMarker.YData = y;
drawnow;
pause(0.01);
end
Which at one point gives the following:

What can I do to make the datatip appear at the location I desire?
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance 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!

