I want to color code US states by specific metrics. What is the easiest way to do this?
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I know how to create a conus map
ax = usamap('conus');
%ax = worldmap('USA');
load coastlines
geoshow(ax, coastlat, coastlon,...
'DisplayType', 'polygon', 'FaceColor', [.45 .60 .30])
states = shaperead('usastatelo', 'UseGeoCoords', true);
faceColors = makesymbolspec('Polygon',...
{'INDEX', [1 numel(states)], 'FaceColor', ...
polcmap(numel(states))}); % NOTE - colors are random
geoshow(ax, states, 'DisplayType', 'polygon', ...
'SymbolSpec', faceColors)
But how do I highlight states by specific metrics such as number of occurrances by specific category?
0 commentaires
Réponse acceptée
Voss
le 3 Juin 2022
Here's how you can make a map colored by log10 of Population Density (2000), which is the variable color_value below. You can change that metric to whatever you want, and the rest of the code is the same.
ax = usamap('conus');
load coastlines
geoshow(ax, coastlat, coastlon,...
'DisplayType', 'polygon', 'FaceColor', [.45 .60 .30])
states = shaperead('usastatelo', 'UseGeoCoords', true)
% - color the states by PopDens2000, since we have that data here in the
% "states" struct array
% - throw out DC since its population density is much higher (~9000) than
% all the actual states (~1.1 to ~1100)
% - throw out Alaska and Hawaii since they won't show up on the map
% (which shows the continental US) - in particular, Alaska's
% population density is the lowest of all the states, and you don't want
% it determining the colors of the other states if Alaska is not even
% shown on the map
states(ismember({states.Name},{'District of Columbia' 'Alaska' 'Hawaii'})) = [];
% use a log color scale:
color_value = log10([states.PopDens2000]);
% pick a colormap to use:
color_map = parula();
% get the min and max color_value:
color_limits = [min(color_value) max(color_value)];
% convert color_value into an index in color_map:
% - minimum color_value goes to 1
% - maximum color_value goes to size(color_map,1)
color_idx = round((color_value-color_limits(1))/(color_limits(2)-color_limits(1))*(size(color_map,1)-1))+1;
% index into color_map using color_idx to
% get the colors to use for each state
faceColors = makesymbolspec('Polygon',{ ...
'INDEX',[1 numel(states)], ...
'FaceColor',color_map(color_idx,:)});
% show the states
geoshow(ax,states,'DisplayType','polygon','SymbolSpec',faceColors);
% make a colorbar
colormap(color_map);
colorbar('YAxisLocation','left');
clim(10.^color_limits);
2 commentaires
Voss
le 6 Juin 2022
Thank you for your prompt and awesome answer. Greatly appreciated.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Purple dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!