Data tip on the centroid of a patch face?
Show older comments
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 Comments
Adam Danz
on 28 Mar 2021
Could you tell us more about how this plot was produced, or better yet, share a minimal working example?
Oleg Iupikov
on 30 Mar 2021
Adam Danz
on 30 Mar 2021
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
on 30 Mar 2021
Adam Danz
on 30 Mar 2021
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
on 30 Mar 2021
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
on 30 Mar 2021
Edited: Oleg Iupikov
on 30 Mar 2021
Accepted Answer
More Answers (0)
Categories
Find more on Polygons 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!
