geoplot - Plot line in geographic coordinates - MATLAB (original) (raw)

Plot line in geographic coordinates

Syntax

Description

Vector Data

geoplot([lat](#mw%5F263267ff-3e1c-4ef3-afe6-51c418fb861c),[lon](#mw%5F974d2941-98e9-4aa7-b1ec-7f5024c0a592)) plots a line in geographic coordinates. Specify latitude coordinates in degrees using lat, and specify longitude coordinates in degrees using lon. If the current axes is not a geographic or map axes, or if there is no current axes, then the function plots the line in a new geographic axes.

example

geoplot([lat](#mw%5F263267ff-3e1c-4ef3-afe6-51c418fb861c),[lon](#mw%5F974d2941-98e9-4aa7-b1ec-7f5024c0a592),[LineSpec](#mw%5F72cdff48-e02c-4af9-9960-1bacb7aa52c5%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)) creates the plot using the specified line style, marker symbol, and color.

example

geoplot([lat](#mw%5F263267ff-3e1c-4ef3-afe6-51c418fb861c)1,[lon](#mw%5F974d2941-98e9-4aa7-b1ec-7f5024c0a592)1,...,[lat](#mw%5F263267ff-3e1c-4ef3-afe6-51c418fb861c)N,[lon](#mw%5F974d2941-98e9-4aa7-b1ec-7f5024c0a592)N) plots pairs of latitude and longitude coordinates using multiple lines on the same axes.

example

geoplot([lat](#mw%5F263267ff-3e1c-4ef3-afe6-51c418fb861c)1,[lon](#mw%5F974d2941-98e9-4aa7-b1ec-7f5024c0a592)1,[LineSpec](#mw%5F72cdff48-e02c-4af9-9960-1bacb7aa52c5%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)1,...,[lat](#mw%5F263267ff-3e1c-4ef3-afe6-51c418fb861c)N,[lon](#mw%5F974d2941-98e9-4aa7-b1ec-7f5024c0a592)N,[LineSpec](#mw%5F72cdff48-e02c-4af9-9960-1bacb7aa52c5%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)N) specifies the line style, marker symbol, and color for each line. You can specify LineSpec for some lines and omit it for others. For example, geoplot(lat1,lon1,"o",lat2,lon2) specifies markers for the first line but not for the second line.

Table Data

geoplot([tbl](#mw%5F72cdff48-e02c-4af9-9960-1bacb7aa52c5%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[latvar](#mw%5Fb9ec206a-6174-47f6-be3a-89fcbb0b9242),[lonvar](#mw%5F3122ae6e-7554-48f3-9790-9450787df047)) plots the variables latvar and lonvar from the table tbl. To plot one data set, specify one variable forlatvar and one variable for lonvar. To plot multiple data sets, specify multiple variables forlatvar, lonvar, or both. If both arguments specify multiple variables, they must specify the same number of variables. (Since R2022b)

example

Additional Options

geoplot([ax](#d126e635062),___) displays the plot in the axes specified by ax. Specify the axes as the first argument in any of the previous syntaxes.

geoplot(___,[Name,Value](#namevaluepairarguments)) specifies properties of the chart line using one or more name-value arguments. The properties apply to all the plotted lines. For a list of properties, seeLine Properties.

example

[h](#mw%5Fe48f21b1-2087-4547-8f24-a6e78dde0c66) = geoplot(___) returns a column vector of chart line objects. Each object corresponds to a plotted line. Use h to modify the properties of the plot after creating it. For a list of properties, see Line Properties.

Note

Mapping Toolbox™ extends the functionality of the geoplot (MATLAB®) function. It adds support for displaying points, lines, and polygons with coordinates in any supported geographic or projected coordinate reference system (CRS). For the geoplot (Mapping Toolbox) page, see geoplot (Mapping Toolbox).

Examples

collapse all

Load a MAT file containing coordinates for the perimeter of the contiguous United States. The variables within the MAT file, uslat and uslon, specify latitude and longitude coordinates, respectively, in degrees. Display the coordinates over a topographic basemap.

load usapolygon.mat geoplot(uslat,uslon) geobasemap topographic

Load a MAT file containing coordinates for each contiguous US state. Extract the coordinates for Indiana, Tennessee, and West Virginia.

load usastates.mat

lat1 = usastates(12).Lat; lon1 = usastates(12).Lon;

lat2 = usastates(40).Lat; lon2 = usastates(40).Lon;

lat3 = usastates(46).Lat; lon3 = usastates(46).Lon;

Display the state outlines using three lines. Zoom out by changing the geographic limits.

geoplot(lat1,lon1,lat2,lon2,lat3,lon3) geolimits([33.5 42.2],[-93.0 -75.5])

Specify the latitude and longitude coordinates of several points along a highway in Germany. Display the points as a line over the streets basemap using a black dashed line with circle markers.

lat = [48.915 48.907 48.901 48.893 48.887 48.881 48.875 48.869 48.865]; lon = [10.192 10.188 10.182 10.171 10.166 10.165 10.167 10.173 10.184];

geoplot(lat,lon,"k--o") geobasemap streets

Specify the latitude and longitude coordinates of several points along the Mississippi River.

lat = [32.30 33.92 35.17 36.98 37.69 38.34]; lon = [-91.05 -91.18 -90.09 -89.11 -89.52 -90.37];

Create a line plot from the data. Use the LineSpec option to specify a dashed green line with square markers. Use name-value arguments to specify the line width, marker size, and marker colors.

geoplot(lat,lon,"--gs",LineWidth=2, ... MarkerSize=10,MarkerEdgeColor="b", ... MarkerFaceColor=[0.5 0.5 0.5])

Zoom out by adjusting the latitude and longitude limits of the plot. Then, change the basemap to a terrain basemap.

geolimits([31.6 38.8],[-97 -83]) geobasemap grayterrain

Specify the geographic coordinates and names of several places in Boston.

lat = [42.3501 42.3598 42.3626 42.3668 42.3557]; lon = [-71.0870 -71.0662 -71.0789 -71.0801 -71.0662]; n = [" BACK BAY"," BEACON HILL"," BROAD CANAL"," EAST CAMBRIDGE"," FLAGSTAFF HILL"];

Plot the locations using black star markers over the topographic basemap. Zoom out of the plot by adjusting the limits.

geoplot(lat,lon,"k*") geobasemap topographic geolimits([42.3456 42.3694],[-71.0977 -71.0489])

Display the place names at the same locations.

Since R2022b

A convenient way to plot data from a table is to pass the table to the geoplot function and specify the variables to plot.

Load a MAT file containing cyclone data into the workspace. Create a table using a subset of the data. The table includes latitude and longitude coordinates in the table variables Latitude and Longitude, respectively.

load cycloneTracks tbl = cycloneTracks(cycloneTracks.ID == 1320,:);

Plot the latitude and longitude coordinates over a topographic basemap. Return the Line object as h.

h = geoplot(tbl,"Latitude","Longitude"); geobasemap topographic

Change the marker style and color of the plot by setting the Marker and Color properties.

h.Marker = "."; h.Color = "m";

Input Arguments

collapse all

Latitude coordinates in degrees, specified as a vector with elements in the range [–90, 90].

Create breaks in the lines using NaN values. For example, this code plots the first two elements, skips the third element, and then plots another line using the last two elements.

geoplot([10 20 NaN 30 40],[10 20 NaN 30 40])

The sizes of lat and lon must match.

Example: [43.0327 38.8921 44.0435]

Data Types: single | double

Longitude coordinates in degrees, specified as a vector.

Create breaks in the lines using NaN values. For example, this code plots the first two elements, skips the third element, and then plots another line using the last two elements.

geoplot([10 20 NaN 30 40],[10 20 NaN 30 40])

The sizes of lat and lon must match.

Example: [-107.5556 -77.0269 -72.5565]

Data Types: single | double

Line style, marker, and color, specified as a string scalar or character vector containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

Example: "--or" is a red dashed line with circle markers.

Line Style Description Resulting Line
"-" Solid line Sample of solid line
"--" Dashed line Sample of dashed line
":" Dotted line Sample of dotted line
"-." Dash-dotted line Sample of dash-dotted line, with alternating dashes and dots
Marker Description Resulting Marker
"o" Circle Sample of circle marker
"+" Plus sign Sample of plus sign marker
"*" Asterisk Sample of asterisk marker
"." Point Sample of point marker
"x" Cross Sample of cross marker
"_" Horizontal line Sample of horizontal line marker
"|" Vertical line Sample of vertical line marker
"square" Square Sample of square marker
"diamond" Diamond Sample of diamond marker
"^" Upward-pointing triangle Sample of upward-pointing triangle marker
"v" Downward-pointing triangle Sample of downward-pointing triangle marker
">" Right-pointing triangle Sample of right-pointing triangle marker
"<" Left-pointing triangle Sample of left-pointing triangle marker
"pentagram" Pentagram Sample of pentagram marker
"hexagram" Hexagram Sample of hexagram marker
Color Name Short Name RGB Triplet Appearance
"red" "r" [1 0 0] Sample of the color red
"green" "g" [0 1 0] Sample of the color green
"blue" "b" [0 0 1] Sample of the color blue
"cyan" "c" [0 1 1] Sample of the color cyan
"magenta" "m" [1 0 1] Sample of the color magenta
"yellow" "y" [1 1 0] Sample of the color yellow
"black" "k" [0 0 0] Sample of the color black
"white" "w" [1 1 1] Sample of the color white

Source table containing the data to plot, specified as a table or a timetable.

Table variables containing the latitude coordinates, specified using one of the indexing schemes from the table.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Regardless of variable names, the axis label on the plot is alwaysLatitude.

The variables must contain numeric data of type single or double. The data must be in the range [–90, 90].

If latvar and lonvar both specify multiple variables, the number of variables must be the same.

Example: geoplot(tbl,["lat1","lat2"],"lon") specifies the table variables named lat1 andlat2 for the latitude coordinates.

Example: geoplot(tbl,2,"lon") specifies the second variable for the latitude coordinates.

Example: geoplot(tbl,vartype("numeric"),"lon") specifies all numeric variables for the latitude coordinates.

Table variables containing the longitude coordinates, specified using one of the indexing schemes from the table.

Indexing Scheme Examples
Variable names: A string, character vector, or cell array.A pattern object. "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable
Variable type: A vartype subscript that selects variables of a specified type. vartype("categorical") — All the variables containing categorical values

Regardless of variable names, the axis label on the plot is alwaysLongitude.

The variables you specify must contain numeric data of typesingle or double.

If latvar and lonvar both specify multiple variables, the number of variables must be the same.

Example: geoplot(tbl,"lat",["lon1","lon2"]) specifies the table variables named lon1 andlon2 for the longitude coordinates.

Example: geoplot(tbl,"lat",2) specifies the second variable for the longitude coordinates.

Example: geoplot(tbl,"lat",vartype("numeric")) specifies all numeric variables for the longitude coordinates.

Target axes, specified as a GeographicAxes object1 or MapAxes (Mapping Toolbox) object. If you do not specify this argument, then thegeoplot function plots into the current axes, provided that the current axes is a geographic or map axes object.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: geoplot(lat,lon,LineWidth=2) plots a line with a line width of 2 points.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: geoplot(lat,lon,"LineWidth",2) plots a line with a line width of 2 points.

Note

Use name-value arguments to specify values for the properties of theLine objects created by this function. The properties listed here are only a subset. For a full list, see Line Properties.

Property settings apply to all lines in the plot. To set the properties of an individual line, get a handle to the line by specifying the output argumenth. Then, use dot notation to set properties for an individual line.

Line color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name.

For a custom color, specify an RGB triplet or a hexadecimal color code.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white
"none" Not applicable Not applicable Not applicable No color

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Example: "blue"

Example: [0 0 1]

Example: "#0000FF"

Marker symbol, specified as one of the markers in this table. By default, a chart line does not have markers. Add markers at each data point along the line by specifying a marker symbol.

Marker Description Resulting Marker
"o" Circle Sample of circle marker
"+" Plus sign Sample of plus sign marker
"*" Asterisk Sample of asterisk marker
"." Point Sample of point marker
"x" Cross Sample of cross marker
"_" Horizontal line Sample of horizontal line marker
"|" Vertical line Sample of vertical line marker
"square" Square Sample of square marker
"diamond" Diamond Sample of diamond marker
"^" Upward-pointing triangle Sample of upward-pointing triangle marker
"v" Downward-pointing triangle Sample of downward-pointing triangle marker
">" Right-pointing triangle Sample of right-pointing triangle marker
"<" Left-pointing triangle Sample of left-pointing triangle marker
"pentagram" Pentagram Sample of pentagram marker
"hexagram" Hexagram Sample of hexagram marker
"none" No markers Not applicable

Marker size, specified as a positive value in points, where 1 point = 1/72 of an inch.

Output Arguments

collapse all

Geographic plot, returned as a column vector of Line objects. Each object corresponds to a plotted line. Useh to modify the properties of the objects after they are created.

Tips

Version History

Introduced in R2018b

expand all

Create a plot by passing a table to the geoplot function followed by the variables you want to plot.

When you plot into geographic axes by using functions such as geoplot orgeoscatter, MATLAB does not reset the basemap. In R2022a and earlier releases, the basemap resets when you add new plots.

As a result, you can specify a basemap and then visualize data without using the hold function between commands. For example, this code creates a map using the streets basemap. Then it displays a plot over the basemap. In R2022b, the basemap does not reset. In R2022a and earlier releases, the basemap resets to the default streets-light.

lat = [35 -22 51 39 37 42 47 -33]; lon = [139 -43 0 116 23 -71 -122 18];

figure geobasemap streets geoplot(lat,lon,"m*")

Two geographic plots. The first plot was created in R2022b, and it displays the specified streets basemap. The second plot was created in R2022a, and it displays the streets-light basemap.

This change does not affect existing code that sets the hold state to "on" between commands.

To reset the basemap when you add a new plot, use the cla reset syntax of the cla function before you create the plot. For example, to update the preceding code, use cla reset between the calls to geobasemap and geoplot.

lat = [35 -22 51 39 37 42 47 -33]; lon = [139 -43 0 116 23 -71 -122 18];

figure geobasemap streets cla reset geoplot(lat,lon,"m*")

Alternatively, you can change the basemap to the default streets-light by using the geobasemap function. For more information about changing the basemap of geographic axes, see Access Basemaps for Geographic Axes and Charts.


1 Alignment of boundaries and region labels are a presentation of the feature provided by the data vendors and do not imply endorsement by MathWorks®.