MATLAB Answers

How to create USA map with states names having different colors?

30 views (last 30 days)
Chetan Badgujar
Chetan Badgujar on 5 Nov 2020
Answered: Asvin Kumar on 18 Mar 2021
Hi,
I want to generate the USA map as follows with the help of mapping toolbox. It should include the Alaska and Hawai. Ans also be able to give the different color based on data and customized number as shown here.

Answers (1)

Asvin Kumar
Asvin Kumar on 18 Mar 2021
usamap is the function you're looking for. Have a look at the example on diplaying the map of USA including Alaska and Hawaii.
Here's an updated version of the code from the example:
% Map the USA Including Alaska and Hawaii
% Map the USA with separate axes for Alaska and Hawaii.
figure
ax = usamap('all');
set(ax, 'Visible', 'off')
states = shaperead('usastatelo', 'UseGeoCoords', true);
names = {states.Name};
% Indices
indexHawaii = strcmp('Hawaii',names);
indexAlaska = strcmp('Alaska',names);
statesSubset1 = {'Illinois', 'Texas'}; % Edit this list
indices = cellfun(@(x) strcmp(x,names), statesSubset1, 'UniformOutput', false);
indicesSubset1 = indices{1};
for i = 2:numel(indices)
indicesSubset1 = indicesSubset1 | indices{i} ;
end
indexConus = 1:numel(states);
indexConus(indexHawaii|indexAlaska|indicesSubset1) = [];
% Colours
stateColor1 = [0.5 1 0.5]; % Edit colours as needed
stateColor2 = [1 0 0];
stateColor3 = [0 0 1];
% Display the three regions.
geoshow(ax(1), states(indexConus), 'FaceColor', stateColor1)
geoshow(ax(1), states(indicesSubset1), 'FaceColor', stateColor2)
geoshow(ax(2), states(indexAlaska), 'FaceColor', stateColor3)
geoshow(ax(3), states(indexHawaii), 'FaceColor', stateColor3)
%Hide the frame.
for k = 1:3
setm(ax(k), 'Frame', 'off', 'Grid', 'off',...
'ParallelLabel', 'off', 'MeridianLabel', 'off')
end
Image of USA with IL and TX in red
You should be able to adapt this code to your use case.
  1. You can add more subsets.
  2. You can update details on which state should belong to which subset.
  3. You can also assign different colours to each subset.
More details on the three Axes objects in the output of usamap is given here.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!