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

22 visualizaciones (últimos 30 días)
Chetan Badgujar
Chetan Badgujar el 5 de Nov. de 2020
Respondida: Asvin Kumar el 18 de Mzo. de 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.

Respuestas (1)

Asvin Kumar
Asvin Kumar el 18 de Mzo. de 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.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by