Incorrect datatip location on interpolated data
조회 수: 7 (최근 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!