spy - Visualize sparsity pattern of matrix - MATLAB (original) (raw)
Main Content
Visualize sparsity pattern of matrix
Syntax
Description
spy([S](#mw%5Fe75a4ddd-92fb-4f8c-8025-684dac464eb1))
plots the sparsity pattern of matrixS
. Nonzero values are colored while zero values are white. The plot displays the number of nonzeros in the matrix, nz = nnz(S)
.
spy([S](#mw%5Fe75a4ddd-92fb-4f8c-8025-684dac464eb1),[LineSpec](#mw%5Fb163bd50-76a0-460a-9d99-e9d926196963))
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](#mw%5F0564a48d-a9ba-4656-a5f3-54a1322a485a))
specifiesMarkerSize
to give the size of the markers using either of the previous input argument combinations.
Examples
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.
Specify the color and marker.
Also specify the marker size.
Input Arguments
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.
Marker | Description | Resulting Marker |
---|---|---|
"o" | Circle | ![]() |
"+" | Plus sign | ![]() |
"*" | Asterisk | ![]() |
"." | Point | ![]() |
"x" | Cross | ![]() |
"_" | Horizontal line | ![]() |
"|" | Vertical line | ![]() |
"square" | Square | ![]() |
"diamond" | Diamond | ![]() |
"^" | Upward-pointing triangle | ![]() |
"v" | Downward-pointing triangle | ![]() |
">" | Right-pointing triangle | ![]() |
"<" | Left-pointing triangle | ![]() |
"pentagram" | Pentagram | ![]() |
"hexagram" | Hexagram | ![]() |
Color Name | Short Name | Appearance |
---|---|---|
'red' | 'r' | ![]() |
'green' | 'g' | ![]() |
'blue' | 'b' | ![]() |
'cyan' | 'c' | ![]() |
'magenta' | 'm' | ![]() |
'yellow' | 'y' | ![]() |
'black' | 'k' | ![]() |
'white' | 'w' | ![]() |
Tips
format +
is a text-based alternative for displaying the nonzero structure of a small matrix:
Extended Capabilities
The spy
function supports GPU array input with these usage notes and limitations:
- This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
The aspect ratio of spy
plots has a 1-to-10 limit, after which the plot stops adjusting to the matrix shape. You can use pbaspect("auto")
for no special aspect ratio or pbaspect([size(A,2) size(A,1) 1])
for the previous behavior of a matching aspect ratio, even for very "squeezed" cases.
Additionally, the default marker size is based only on the matrix size. Previously, the point size of the axes on creation could also affect the marker size.