Contenido principal

geodensityplot

Density plot in geographic coordinates

  • Density plot in geographic coordinates

Description

Vector Data

geodensityplot(lat,lon) creates a density plot in geographic coordinates. Specify the latitude coordinates in degrees using lat, and specify the longitude coordinates in degrees using lon. If the current axes is not a geographic axes, or if there is no current axes, then the function creates the density plot in a new geographic axes.

example

geodensityplot(lat,lon,weights) specifies weights for the data points.

example

Table Data

geodensityplot(tbl,latvar,lonvar) creates a density plot using the variables latvar and lonvar from the table tbl. (since R2026a)

example

geodensityplot(tbl,latvar,lonvar,wvar) specifies weights for the data points using the table variable wvar. (since R2026a)

Additional Options

geodensityplot(gx,___) plots into the geographic axes specified by gx. Specify the axes as the first argument followed by any of the input argument combinations in the previous syntaxes.

geodensityplot(___,Name=Value) specifies properties of the density plot using one or more name-value arguments. For a list of properties, see DensityPlot Properties.

example

dp = geodensityplot(___) returns the DensityPlot object. Use dp to set properties after creating the plot. For a full list of properties, see DensityPlot Properties.

Examples

collapse all

Load a table containing cell tower data for California. Each table row represents a cell tower. The table variables include data about the cell towers, such as the latitude and longitude coordinates. Extract the latitude and longitude coordinates from the table.

load cellularTowers.mat
lat = cellularTowers.Latitude;
lon = cellularTowers.Longitude;

Create a density plot from the coordinates. By default, the geodensityplot function visualizes density by varying the transparency of the plot. Regions with high density are more opaque, and regions with low density are more transparent.

figure
geodensityplot(lat,lon)

Load a table containing cyclone track data. The table includes the locations and wind speeds of over 200 cyclones, measured at six-hour intervals. Extract the latitude coordinates, the longitude coordinates, and the wind speeds from the table.

load cycloneTracks.mat
lat = cycloneTracks.Latitude;
lon = cycloneTracks.Longitude;
windspeed = cycloneTracks.WindSpeed;

Create a density plot from the coordinates, and weight the points using the wind speeds. The resulting density plot highlights the areas where the cyclones have the highest wind speeds.

geodensityplot(lat,lon,windspeed)

This example uses modified RSMC Best Track Data from the Japan Meteorological Agency.

Load a table containing cell tower data for California. Each table row represents a cell tower. The table variables include data about the cell towers, such as the latitude and longitude coordinates. Extract the latitude and longitude coordinates from the table.

load cellularTowers.mat
lat = cellularTowers.Latitude;
lon = cellularTowers.Longitude;

Create a density plot from the coordinates. Specify the radius of influence for each point as 50 km.

geodensityplot(lat,lon,Radius=50e3)

By default, the geodensityplot function visualizes density by varying the transparency of the density plot. You can also visualize density by varying the plot colors.

Load a table containing cyclone track data. The table records the tracks of over 200 cyclones, measured at six-hour intervals. Extract the latitude and longitude coordinates from the table.

load cycloneTracks.mat
lat = cycloneTracks.Latitude;
lon = cycloneTracks.Longitude;

Create a density plot from the coordinates. Vary the plot colors by setting the FaceColor property to "interp".

geodensityplot(lat,lon,FaceColor="interp")

Change the colormap, and add a labeled color bar. When you do not weight the data, the units of the density plot are points per square meter. Note that the plot visualizes the density using both transparency and color.

colormap turbo
c = colorbar;
c.Label.String = "Data points per square meter";

This example uses modified RSMC Best Track Data from the Japan Meteorological Agency.

Since R2026a

You can plot data directly from a table by passing the table to the geodensityplot function and specifying the variables to plot.

Load a table containing cell tower data for California. The table stores the latitude and longitude coordinates in the table variables Latitude and Longitude, respectively.

load cellularTowers.mat

Create a density plot from the table. Return the DensityPlot object as dp.

dp = geodensityplot(cellularTowers,"Latitude","Longitude");

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type densityplot.

Change the face color of the plot by setting the FaceColor property.

dp.FaceColor = "m";

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type densityplot.

Input Arguments

collapse all

Latitude coordinates in degrees, specified as a vector with elements in the range [–90, 90]. The vector can contain NaN values.

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. The vector can contain NaN values.

The sizes of lat and lon must match.

Example: [-107.5556 -77.0269 -72.5565]

Data Types: single | double

Weights assigned to the data, specified as a numeric scalar, a numeric vector, or an empty array ([]). If you specify a vector, the size of the vector must match the sizes of lat and lon.

This argument typically specifies additional data that is related to the location data in lat and lon.

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

Since R2026a

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

Indexing SchemeExamples

Variable name:

  • A string scalar or character vector.

  • A pattern object. The pattern object must refer to only one variable.

  • "A" or 'A' — A variable named A

  • "Var"+digitsPattern(1) — The variable with the name "Var" followed by a single digit

Variable index:

  • An index number that refers to the location of a variable in the table.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects a table variable of a specified type. The subscript must refer to only one variable.

  • vartype("double") — The variable containing double values

Regardless of the variable name, the axis label on the plot is always Latitude.

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

Example: geodensityplot(tbl,"lat","lon") specifies the table variable named lat for the latitude coordinates.

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

Since R2026a

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

Indexing SchemeExamples

Variable name:

  • A string scalar or character vector.

  • A pattern object. The pattern object must refer to only one variable.

  • "A" or 'A' — A variable named A

  • "Var"+digitsPattern(1) — The variable with the name "Var" followed by a single digit

Variable index:

  • An index number that refers to the location of a variable in the table.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects a table variable of a specified type. The subscript must refer to only one variable.

  • vartype("double") — The variable containing double values

Regardless of the variable name, the axis label on the plot is always Longitude.

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

Example: geodensityplot(tbl,"lat","lon") specifies the table variable lon for the longitude coordinates.

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

Since R2026a

Table variable containing the weights assigned to the data, specified using one of the indexing schemes from this table.

Indexing SchemeExamples

Variable name:

  • A string scalar or character vector.

  • A pattern object. The pattern object must refer to only one variable.

  • "A" or 'A' — A variable named A

  • "Var"+digitsPattern(1) — The variable with the name "Var" followed by a single digit

Variable index:

  • An index number that refers to the location of a variable in the table.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects a table variable of a specified type. The subscript must refer to only one variable.

  • vartype("double") — The variable containing double values

The variable you specify must contain numeric data.

Example: geodensityplot(tbl,"lat","lon","wgt") specifies the table variable named wgt for the weights.

Example: geodensityplot(tbl,"lat","lon",2) specifies the second table variable for the weights.

Target geographic axes, specified as a GeographicAxes object.1 You can modify the appearance and behavior of a GeographicAxes object by setting its properties. For a list of properties, see GeographicAxes Properties.

If you do not specify this argument, then the function plots into the current axes, provided that the current axes is a geographic axes. If the current axes is not a geographic axes, or if there is no current axes, then the function plots into a new geographic axes.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=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: geodensityplot(lat,lon,FaceColor="g") sets the face color of the density plot to green.

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

Example: geodensityplot(lat,lon,"FaceColor","g") sets the face color of the density plot to green.

Note

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

Radius of influence on the density calculation, in meters, specified as a numeric scalar.

Face transparency, specified as one of these values:

  • 'interp' — Use interpolated transparency based on the density values.

  • Scalar in the range [0, 1] — Use uniform transparency across all the faces. A value of 1 is opaque and a value of 0 is completely transparent. Values between 0 and 1 are semitransparent.

The appearance of the density plot depends on both the FaceAlpha and FaceColor properties. This table shows how different combinations of FaceAlpha and FaceColor affect the appearance of the plot.

Values of FaceColor and FaceAlphaEffectSample Density Plot
  • FaceAlpha is "interp"

  • FaceColor is an RGB triplet, a hexadecimal color code, a color name, or a short name

The density plot uses one color and conveys density by varying the transparency.

Density plot in blue with varying transparency

  • FaceAlpha is "interp"

  • FaceColor is "interp"

The density plot conveys density by varying the transparency and the color.

Density plot with varying transparency and a colormap that starts at dark blue and transitions to light blue, bright green, orange, yellow, and dark red. The blue regions of the plot are almost completely transparent. The red regions of the plot are opaque.

  • FaceAlpha is a scalar value

  • FaceColor is "interp"

The density plot uses one transparency value and conveys density by varying the color.

Density plot with consistent opacity and a colormap that starts at dark blue and transitions to light blue, bright green, orange, yellow, and dark red. The opacity makes the density plot look like a rectangle.

For more information about controlling the transparency of a density plot, see Adjust Transparency of Geographic Density Plots.

Face color, specified as one of these options:

  • 'interp' — Use interpolated coloring based on the density values. MATLAB® chooses colors from the colormap of the parent axes. When you choose this option, the appearance of the density plot also depends on the value of the FaceAlpha property. For more information, see the FaceAlpha property.

  • An RGB triplet, a hexadecimal color code, a color name, or a short name — Apply one color to the density plot. When you choose this option, the value of FaceAlpha must be "interp".

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

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

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"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

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

PalettePalette 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)).

Tips

When you plot on geographic axes, the geodensityplot function assumes that coordinates are referenced to the WGS84 coordinate reference system. If you plot using coordinates that are referenced to a different coordinate reference system, then the coordinates might appear misaligned.

Algorithms

A density plot is a surface with varying transparency. The geodensityplot function creates the surface by calculating a cumulative probability distribution from the specified points and varying the transparency with the density of the points.

By default, each point contributes equally to the density plot. When you weight the points, the function multiplies the contribution of the associated points to the density plot.

Alternative Functionality

A density plot is a type of heatmap. Other types of heatmaps that you can create on maps include:

Version History

Introduced in R2018b

expand all


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®.