Display Vector Data as Lines and Patches
This example shows how to display vector data on map axes using lines or patches (filled polygons). Map axes enable you to display geographic data using a map projection.
Load data containing the latitude and longitude coordinates for these polygon and line shapes. The data separates individual polygons and lines using NaN
values.
uslat
anduslon
— Three polygons representing the conterminous United States, Long Island, and Martha's Vineyard.gtlakelat
andgtlakelon
— Three polygons representing the Great Lakes.statelat
andstatelon
— Lines representing the borders between states.
load conus
Use the coordinate data to create polygon and line shapes. Combine the shapes into a geospatial table.
us = geopolyshape(uslat,uslon);
states = geopolyshape(gtlakelat,gtlakelon);
gtlake = geolineshape(statelat,statelon);
conusGT = table([us; states; gtlake],VariableNames="Shape")
conusGT=3×1 table
Shape
____________
geopolyshape
geopolyshape
geolineshape
Read a river network into a geospatial table containing line shapes. Clip the line shapes to a region that covers the conterminous United States.
riversGT = readgeotable("worldrivers.shp");
uslatlim = [min(uslat) max(uslat)];
uslonlim = [min(uslon) max(uslon)];
clipped = geoclip(riversGT.Shape,uslatlim,uslonlim);
riversGT.Shape = clipped;
Set up a map axes to display the state coordinates, turning on the map frame, map grid, and the meridian and parallel labels. As conic projections are appropriate for mapping the entire United States, create a map axes object using an Albers equal-area conic projection ( "eqaconic"
). Specifying map limits that contain the region of interest automatically centers the projection on an appropriate longitude. The frame encloses just the mapping area, not the entire globe. As a general rule, you should specify map limits that extend slightly outside your area of interest (worldmap
and usamap
do this for you). Conic projections need two standard parallels (latitudes at which scale distortion is zero). A good rule is to set the standard parallels at one-sixth of the way from both latitude extremes. Or, to use default latitudes for the standard parallels, simply provide an empty matrix in the call to axesm
.
figure axesm("MapProjection","eqaconic","MapParallels",[], ... "MapLatLimit",uslatlim + [-2 2], ... "MapLonLimit",uslonlim + [-2 2]) axis off framem gridm mlabel plabel
Display the area occupied by the conterminous United States as a patch. Note that the order in which add layers to a map can affect visibility because some layers can hide other layers. For example, because some US state boundaries follow major rivers, display the rivers last to avoid obscuring them.
geoshow(conusGT(1,:),"FaceColor",[1 0.5 0.3],"EdgeColor","none")
Display the Great Lakes on top of the land area.
geoshow(conusGT(2,:),"FaceColor","cyan", "EdgeColor","none")
Display the line shapes containing state boundaries.
geoshow(conusGT(3,:),"Color","k")
Display the river network.
geoshow(riversGT,"Color","blue")
Tips
To plot data that is already in projected coordinates, use a regular axes object and the
mapshow
function.Starting in R2022a, you can create a similar plot by using geographic axes and the
geoplot
function. Geographic axes enable you to specify basemaps, change the geographic limits, and interactively pan and zoom. Thegeoplot
function accepts shapes with coordinates in any supported projected or geographic coordinate reference system (CRS) and displays the data into a Web Mercator map projection.
figure geoplot(conusGT(1,:),"FaceColor",[1 0.5 0.3],"EdgeColor","none") hold on geoplot(conusGT(2,:),"FaceColor","cyan","EdgeColor","none") geoplot(conusGT(3,:),"k") geoplot(riversGT,"Color","blue") alpha(1) geobasemap topographic