Incorrect datatip location on interpolated data
5 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Joseph Cheng
2021-6-21
编辑:Joseph Cheng
2021-6-21
so you can see what is happening if you plot it as just markers in this animated example:
figure; hold on;
hPlot = plot(1:10, [1:10]);
hMarker = scatter(1,1, 'ro');
hDataTip = datatip(hPlot, 'SnapToDataVertex', 'off');
n=1;
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.0001);
data(n,:)=[x y hDataTip.X hDataTip.Y];
% disp([x y hDataTip.X hDataTip.Y])
n=n+1;
end
figure,plot(data(:,1),data(:,3));xlabel('desired x val'),ylabel('datatip x val')
where the marker wants to snap to the nearest point in the specified hPlot input line given in the hDataTip creation. i can't quite see quickly what it is wanted to do but when you plot it as a line instead of using just the marker. Seems a bit inconsistent for the 1~2 min i looked at it and trying to compare the extracted by some rounded integer value + keeping the decimal values. as you can see in the desired versus data tip x values at 1.5 the values jump to 2.5
to get around this you should specify the interpolated marker as where you want to put the data tip against:
figure; hold on;
hPlot = plot(1:10, 1:10,'o');
hMarker = scatter(1,1, 'ro');
hDataTip = datatip(hMarker, 'SnapToDataVertex', 'off');
for(x = 1:0.04:5)
y = interp1(hPlot(1).XData, hPlot(1).YData, x); % Get y-coordinate at given x.
hDataTip.X = x;
hDataTip.Y = y;
hMarker.XData = x;
hMarker.YData = y;
drawnow;
pause(0.1);
disp([x y])
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!