Interactively show polygon label on choropleth map
I need to map a polygon shapefile in a choropleth map and interactively show the name of the specific polygon on which I click with the mouse. The default data tool tip does not work for me because: (1) information gets displayed for each polygon vertex, while I need the information only when clicking in the inner part of the polygon; (2) apparently, the only information it can show are coordinates.
To give an idea of what I am after, I produce hereby a minimum working example. In the last line of this example, I add all polygon names via the text command, associating each polygon to a unique inner point. This determines the placement of the text. What I need, instead, is for such text to be displayed only for the single polygon on which I click with the mouse.
Any help would be greatly appreciated.
% Minimum working example
% pgn = shaperead(‘pgn.shp’);
% pts = shaperead(‘pts.shp’);
load(‘pgn.mat’)
load(‘pts.mat’)
colorgradient = parula(height(pgn));
cbarlim = [floor(min([pgn.MA1911])) ceil(max([pgn.MA1911]))];
symbpgn = makesymbolspec(‘Polygon’, …
{‘MA1911’, cbarlim, ‘FaceColor’, colorgradient});
mapshow(pgn,’SymbolSpec’,symbpgn);
disableDefaultInteractivity(gca) % This prevents polygon vertex coordinates from showing in the tooltip
ax = gca;
ax.CLim = cbarlim;
h = colorbar;
h.Label.String = ‘people’;
axis equal
axis off
hold on
text([pts.X]’,[pts.Y]’,{pts.UNIT}’) % This is what needs to become interactiveI need to map a polygon shapefile in a choropleth map and interactively show the name of the specific polygon on which I click with the mouse. The default data tool tip does not work for me because: (1) information gets displayed for each polygon vertex, while I need the information only when clicking in the inner part of the polygon; (2) apparently, the only information it can show are coordinates.
To give an idea of what I am after, I produce hereby a minimum working example. In the last line of this example, I add all polygon names via the text command, associating each polygon to a unique inner point. This determines the placement of the text. What I need, instead, is for such text to be displayed only for the single polygon on which I click with the mouse.
Any help would be greatly appreciated.
% Minimum working example
% pgn = shaperead(‘pgn.shp’);
% pts = shaperead(‘pts.shp’);
load(‘pgn.mat’)
load(‘pts.mat’)
colorgradient = parula(height(pgn));
cbarlim = [floor(min([pgn.MA1911])) ceil(max([pgn.MA1911]))];
symbpgn = makesymbolspec(‘Polygon’, …
{‘MA1911’, cbarlim, ‘FaceColor’, colorgradient});
mapshow(pgn,’SymbolSpec’,symbpgn);
disableDefaultInteractivity(gca) % This prevents polygon vertex coordinates from showing in the tooltip
ax = gca;
ax.CLim = cbarlim;
h = colorbar;
h.Label.String = ‘people’;
axis equal
axis off
hold on
text([pts.X]’,[pts.Y]’,{pts.UNIT}’) % This is what needs to become interactive I need to map a polygon shapefile in a choropleth map and interactively show the name of the specific polygon on which I click with the mouse. The default data tool tip does not work for me because: (1) information gets displayed for each polygon vertex, while I need the information only when clicking in the inner part of the polygon; (2) apparently, the only information it can show are coordinates.
To give an idea of what I am after, I produce hereby a minimum working example. In the last line of this example, I add all polygon names via the text command, associating each polygon to a unique inner point. This determines the placement of the text. What I need, instead, is for such text to be displayed only for the single polygon on which I click with the mouse.
Any help would be greatly appreciated.
% Minimum working example
% pgn = shaperead(‘pgn.shp’);
% pts = shaperead(‘pts.shp’);
load(‘pgn.mat’)
load(‘pts.mat’)
colorgradient = parula(height(pgn));
cbarlim = [floor(min([pgn.MA1911])) ceil(max([pgn.MA1911]))];
symbpgn = makesymbolspec(‘Polygon’, …
{‘MA1911’, cbarlim, ‘FaceColor’, colorgradient});
mapshow(pgn,’SymbolSpec’,symbpgn);
disableDefaultInteractivity(gca) % This prevents polygon vertex coordinates from showing in the tooltip
ax = gca;
ax.CLim = cbarlim;
h = colorbar;
h.Label.String = ‘people’;
axis equal
axis off
hold on
text([pts.X]’,[pts.Y]’,{pts.UNIT}’) % This is what needs to become interactive mapshow, tooltip MATLAB Answers — New Questions