4
votes

I want to plot different locations on a map of NY state. My current code plots the entirety of North America because I couldn't find how to plot just one state. I'm trying to set the latitude and longitude limits to the state of NY, but it's still giving me the entire country.

In addition, when I do hold all (or hold on) and try to plot the points, I get another figure that pops up with the title I specified, but just a blank, white square.

Another related question is that once I get the points plotted, I have to know what points they are. If I have the names in a separate cell array, how can I have MATLAB label the points based on which coordinates (which are stored in a separate column) correspond with which name (if the names are stored in yet another column)?

%% Plot map
latlim = [39 47];
lonlim = [-81 -70];

ax = worldmap('USA');
load coast
geoshow(ax, lat, long,...
'DisplayType', 'polygon', 'FaceColor', [.45 .60 .30])
states = shaperead('usastatelo', 'UseGeoCoords', true, 'BoundingBox', [lonlim' latlim']);
axesm('lambert', 'MapLatLimit', latlim, 'MapLonLimit', lonlim);
faceColors = makesymbolspec('Polygon',...
    {'INDEX', [1 numel(states)], 'FaceColor', ...
    polcmap(numel(states))}); % NOTE - colors are random
geoshow(ax, states, 'DisplayType', 'polygon', ...
  'SymbolSpec', faceColors);
figure('Color', 'white')

title('PM2.5 Site in New York State in 2012');

hold all

% Plot points
axesm('lambert', 'MapLatLimit', latlim', 'MapLonLimit', lonlim');
datalat = str2double(datalat);
datalon = str2double(datalon);
scatterm(datalat, datalon)
1

1 Answers

3
votes

You can get a USA state map with usamap('New York') and plot an overlay text with textm. Here, 25 random points and their label are plotted on the figure.

The following plot

enter image description here

is produced by

latlim = [39 47];
lonlim = [-81 -70];

figure('Color','w');
usamap('New York')
shi = shaperead('usastatehi', 'UseGeoCoords', true,...
            'Selector',{@(name) strcmpi(name,'New York'), 'Name'});
geoshow(shi, 'FaceColor', [0.3 1.0, 0.675])
textm(shi.LabelLat, shi.LabelLon, shi.Name, 'HorizontalAlignment', 'center')

nb_point = 25;
LAT = latlim(1) + (latlim(2)-latlim(1)).*rand(nb_point,1);
LON = lonlim(1) + (lonlim(2)-lonlim(1)).*rand(nb_point,1);
h = geoshow(LAT, LON, 'DisplayType', 'Point', 'Marker', '+', 'Color', 'red');
textm(LAT, LON+0.3, num2str((1:nb_point)'), 'FontSize',14)