Issue creating a map of the USA
조회 수: 6 (최근 30일)
이전 댓글 표시
I've been trying to figure out how to create a map of the USA (including Alaska and Hawaii) with labels for every state using the usamap function. Matlab only has documentation for how to name specific regions, not the whole country. My code is below - would anybody be able to help me modify it so that I can label the states? Thank you!!!!!!!
% Create a map of the continental United States
figure;
electionMap = usamap('all');
states = shaperead('usastatehi','UseGeoCoords',true);
nameStates = {states.Name};
Conus = 1:numel(states);
Alaska = strcmp(nameStates,'Alaska');
Hawaii = strcmp(nameStates,'Hawaii');
Conus(Hawaii|Alaska) = [];
% Display and format map of US mainland and Alaska and Hawaii
setm(electionMap(1),'Frame','off','Grid','off','MeridianLabel','off','ParallelLabel','off')
geoshow(electionMap(1),states(Conus),'FaceColor','#F6E8B1')
setm(electionMap(2),'Frame','off','Grid','off','MeridianLabel','off','ParallelLabel','off')
geoshow(electionMap(2), states(Alaska),'FaceColor','#F6E8B1')
setm(electionMap(3),'Frame','off','Grid','off','MeridianLabel','off','ParallelLabel','off')
geoshow(electionMap(3), states(Hawaii),'FaceColor','#F6E8B1')
댓글 수: 0
채택된 답변
Cris LaPierre
2020년 12월 9일
figure
ax = usamap('all');
set(ax, 'Visible', 'off')
states = shaperead('usastatelo', 'UseGeoCoords', true);
names = {states.Name};
indexHawaii = strcmp('Hawaii',names);
indexAlaska = strcmp('Alaska',names);
indexConus = 1:numel(states);
indexConus(indexHawaii|indexAlaska) = [];
stateColor = [0.5 1 0.5];
Display the three regions.
geoshow(ax(1), states(indexConus), 'FaceColor', stateColor)
geoshow(ax(2), states(indexAlaska), 'FaceColor', stateColor)
geoshow(ax(3), states(indexHawaii), 'FaceColor', stateColor)
Hide the frame.
for k = 1:3
setm(ax(k), 'Frame', 'off', 'Grid', 'off',...
'ParallelLabel', 'off', 'MeridianLabel', 'off')
end
Add labels to each state.
lat = [states(indexConus).LabelLat];
lon = [states(indexConus).LabelLon];
textm(lat, lon, {states(indexConus).Name},'HorizontalAlignment', 'center','Parent',ax(1));
latA = [states(indexAlaska).LabelLat];
lonA = [states(indexAlaska).LabelLon];
textm(latA, lonA, {states(indexAlaska).Name},'HorizontalAlignment', 'center','Parent',ax(2));
latH = [states(indexHawaii).LabelLat];
lonH = [states(indexHawaii).LabelLon];
textm(latH, lonH, {states(indexHawaii).Name},'HorizontalAlignment', 'center','Parent',ax(3));
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Map Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!