Data tip on the centroid of a patch face?
이전 댓글 표시
Hallo.
Is it possible programmatically create a data tip on a patch face center? It looks like it always sticks to the patch vertices.
The color of this 2D patch represents some data which would be nice to explore using data tip with a custom update function. I know how to write such update function but the problem is that the data tip automatically sticks only to the patch vertices...
Here is the patch example:

Thanks!
----- Update ----
Here is an example:
clc
close all
% Define some data
[xo,yo] = ndgrid(1:4,1:5); % data point positions
data = 100*rand(numel(xo),1); % data point values
% Form patch faces, each for each point of the data
width = 0.8;
x = nan(4,numel(xo));
y = nan(4,numel(xo));
x(1,:) = xo(:) - width/2; y(1,:) = yo(:) - width/2;
x(2,:) = xo(:) - width/2; y(2,:) = yo(:) + width/2;
x(3,:) = xo(:) + width/2; y(3,:) = yo(:) + width/2;
x(4,:) = xo(:) + width/2; y(4,:) = yo(:) - width/2;
% Plot
hFig = figure('color','w');
hPatch = patch('XData',x, 'YData',y, 'ZData',0*x, 'CData',data, 'FaceColor','flat');
colormap jet; colorbar;
% Set custom update function
dcm = datacursormode(hFig);
set(dcm,'UpdateFcn',{@myupdatefcn,data});
% Add tatatip. Here I would like to display the data tip at exact location (2,3) which is middle of the patch face, but
% it is snapped to the face vertex despite the option 'SnapToDataVertex' is 'off'...
% Moreover, it seems the datatip function do not respect the new update function (but this is not important for me now).
% See the figure below.
dt = datatip(hPatch, 2,3, 'SnapToDataVertex','off');
function txt = myupdatefcn(~,evt,data)
pos = get(evt,'Position');
ind = ceil( get(evt, 'DataIndex') / 4 );
txt = { sprintf('(x,y): (%g, %g)', pos(1:2)),...
sprintf('data index: %i', ind),...
sprintf('data value: %g', data(ind))
};
end
Here is result:

First datatip was created by the function datatip which is not at the position (2, 3) despite the option 'SnapToDataVertex' is 'off' (and which do not use the new update function). Second data tip was created by clicking the patch.
How to switch off the snapping?
P.S. MATLAB version R2020b.
댓글 수: 7
Adam Danz
2021년 3월 28일
Could you tell us more about how this plot was produced, or better yet, share a minimal working example?
Oleg Iupikov
2021년 3월 30일
Adam Danz
2021년 3월 30일
You could add very small points to the center of each patch with a color that matches the patch so you cannot see it.
Example:
hold on
plot(1,4,'b.')
Those objects could be the target of your datatips.
Oleg Iupikov
2021년 3월 30일
Adam Danz
2021년 3월 30일
Why are you using patch objects instead of a heat map or imagesc or histogram2? The current approach is very inefficient in memory and render time.
Adam Danz
2021년 3월 30일
If the grid edges are contiguous then imagesc or heatmap is the way to go, and imagesc would likely be the better choice since it's more customizable. If the 2D bin edges are not contiguous then histogram2 might be a better approach, although I haven't entirely through it through.
Oleg Iupikov
2021년 3월 30일
편집: Oleg Iupikov
2021년 3월 30일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

