Clear Filters
Clear Filters

How to add simple rectangle legend inside US map?

6 views (last 30 days)
Hi all,
I'd like to add a simple legend with two rectangles like the following inside the US map (rectangle No shock, another rectangle with different color Shock)
Did anyone know?
Here is my current code.
clear all; clc;
states = shaperead('usastatehi', 'UseGeoCoords', true);
statenames = {states.Name};
alaska = states(strcmp('Alaska', statenames));
hawaii = states(strcmp('Hawaii', statenames));
f = figure;
hconus = usamap('conus');
for i = 1:51
ccolor = [1 1 1];
geoshow(hconus, states(i), 'FaceColor', ccolor)
end
framem off; gridm off; mlabel off; plabel off
halaska = axes('Parent',f);
usamap('alaska')
geoshow(alaska, 'FaceColor', [0.23 0.44 0.67]);
framem off; gridm off; mlabel off; plabel off
hhawaii = axes('Parent',f);
usamap('hawaii')
geoshow(hawaii, 'FaceColor', [1 1 1]);
framem off; gridm off; mlabel off; plabel off
set(hconus, 'Position',[0.1 0.35 0.85 0.6])
set(halaska,'Position',[0.2 0.42 0.2 0.2])
set(hhawaii,'Position',[0.25 0.43 0.2 0.2])
Capture.PNG

Answers (1)

Vahila Kayithi
Vahila Kayithi on 26 Dec 2019
The rectangular legend can be obtained by changing the 'Display Type' property. Create the handles for 'geoshow' and change the 'Display Type' property. Pass the handles to legends.
h(1) = geoshow(alaska, 'FaceColor', [0.23 0.44 0.67],'DisplayType', 'polygon');
h(2) = geoshow(hawaii, 'FaceColor', [0 0 0],'DisplayType', 'polygon') ;
legend([h(1),h(2)],'No Shock','Shock');
For more information, please refer:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!