Add Labels and Annotations to Geographic Axes
Geographic axes are a type of axes, similar to Cartesian axes and polar axes. As a result, you can use many MATLAB® graphics functions with geographic axes. This example shows how to convey information about a map by adding a title, a legend, text, and data tips.
By default, most plotting functions reset many of the axes properties. This example retains the plots and customizations in the axes by setting the hold state of the axes to on
. Alternatively, you can customize the axes after plotting.
Create Map
Load two files into the workspace:
A MAT file containing coordinates that outline the conterminous US states.
A table containing county data for New England.
load("usastates.mat") statelat = [usastates.Lat]; statelon = [usastates.Lon]; counties = readtable("counties.xlsx");
Plot the data by using the geoplot
and geoscatter
functions. The functions display the data using geographic axes.
Create a line plot from the outlines by using the
geoplot
function.Create a scatter plot from the county data by using the
geoscatter
function. Prepare to add data tips by returning theScatter
object ins
.
figure geoplot(statelat,statelon) hold on s = geoscatter(counties,"Latitude","Longitude","filled");
Change the basemap and the geographic limits.
geobasemap grayland
geolimits([40.85 45.73],[-75.00 -66.50])
Add Title
Add a title by using the title
function.
title("New England")
Add Legend
Add a legend with descriptions. Specify the legend labels as inputs to the legend
function. For more information about adding legends to axes, see Add Legend to Graph.
legend(["State Outlines","Counties"])
Change Font Size
Change the font size by using the fontsize
function. The fontsize
function affects the sizes of fonts used in the title, tick labels, and scale bar, as well as in any legends or color bars associated with the axes.
fontsize(12,"points")
To change the font size of only the title, access the title of the current geographic axes object. Then, specify the title as input to the fontsize
function.
gx = gca;
t = gx.Title;
fontsize(t,16,"points")
Add Text
Add text to the map. Specify the location for the text using latitude-longitude coordinates. For more information about adding text to axes, see Add Text to Chart.
text(43,-69,"Atlantic Ocean")
Add Data Tips
Display the coordinates of a county by adding a data tip. Specify the scatter chart and latitude-longitude coordinates as inputs to the datatip
function.
dt = datatip(s,42.64,-70.87,Location="southeast");
Display the county name in the data tip by adding a new row to the data tip template. For more information about customizing data tips, see Create Custom Data Tips.
dtRow = dataTipTextRow("County",counties.CountyName);
s.DataTipTemplate.DataTipRows(end+1) = dtRow;