Main Content


Visualize sparsity pattern of matrix

  • Visualization of the sparsity pattern of matrix



spy(S) plots the sparsity pattern of matrix S. Nonzero values are colored while zero values are white. The plot displays the number of nonzeros in the matrix, nz = nnz(S).


spy(S,LineSpec) additionally specifies LineSpec to give the marker symbol and color to use in the plot. For example, spy(A,'r*') uses red asterisks for nonzeros.


spy(___,MarkerSize) specifies MarkerSize to give the size of the markers using either of the previous input argument combinations.


collapse all

Plot the 60-by-60 sparse adjacency matrix of the connectivity graph of the Buckminster Fuller geodesic dome. This matrix also represents the carbon-60 molecule and a soccer ball.

B = bucky;

Specify the color and marker.


Also specify the marker size.


Input Arguments

collapse all

Input matrix. S is typically a sparse matrix, but can be either full or sparse.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Number Support: Yes

Size of markers, specified as a positive integer scalar.

Example: spy(A,3) uses markers of size 3.

Marker symbol and color, specified as a character vector or string containing symbols. The symbols can appear in any order. You do not need to specify both characteristics (marker and color). For example, if you omit the color and specify the marker, then the plot uses the default colors but uses the specified markers.

Example: 'or' uses red circle markers.

MarkerDescriptionResulting Marker

Sample of circle marker

"+"Plus sign

Sample of plus sign marker


Sample of asterisk marker


Sample of point marker


Sample of cross marker

"_"Horizontal line

Sample of horizontal line marker

"|"Vertical line

Sample of vertical line marker


Sample of square marker


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


Sample of pentagram marker


Sample of hexagram marker

Color NameShort NameAppearance

Sample of the color red


Sample of the color green


Sample of the color blue

'cyan' 'c'

Sample of the color cyan


Sample of the color magenta


Sample of the color yellow


Sample of the color black


Sample of the color white


  • format + is a text-based alternative for displaying the nonzero structure of a small matrix:

    format +
    ans =

Extended Capabilities

Version History

Introduced before R2006a

expand all