({{, (RefLink[MatrixPlot, paclet:ref/MatrixPlot])[m] 
generates a plot that gives a visual representation of the values of elements in a matrix.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[MatrixPlot, paclet:ref/MatrixPlot])[m] by default arranges successive rows of m down the page and successive columns across, just as a matrix would normally be formatted.

(RefLink[MatrixPlot, paclet:ref/MatrixPlot]) by default displays zero values as white, with negative values tending to be bluish and positive values reddish.

(RefLink[MatrixPlot, paclet:ref/MatrixPlot]) has the same options as (RefLink[ArrayPlot, paclet:ref/ArrayPlot]), with the following changes:

(RefLink[PlotRange, paclet:ref/PlotRange])->r specifies that only those (Subscript[a, ij]) between -r and +r should be shown.

With the default setting (RefLink[ColorFunctionScaling, paclet:ref/ColorFunctionScaling])->(RefLink[True, paclet:ref/True]), scaling is done based on a mixture of relative value and ranking for each matrix element. The final scaled value always lies between 0 and 1, with scaled value 0.5 corresponding to matrix element value 0.

With the default setting (RefLink[MaxPlotPoints, paclet:ref/MaxPlotPoints])->(RefLink[Automatic, paclet:ref/Automatic]), sufficiently large or sparse matrices are downsampled so that their structure is visible in the plot generated by (RefLink[MatrixPlot, paclet:ref/MatrixPlot]).

(RefLink[MatrixPlot, paclet:ref/MatrixPlot]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

OpenersPrimaryExamplesSection(Spacer1)Examples  (73)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Plot a matrix as an array of colors:

MatrixPlot[{{1,2,1},{3,0,1},{0,0,-1}}]

Plot a matrix using only black and white:

MatrixPlot[{{1,2,1},{3,0,1},{0,0,-1}},ColorFunction->"Monochrome"]

Plot the structure of dense matrices:

MatrixPlot[Fourier[Table[UnitStep[i,4-i] UnitStep[j,7-j],{i,-50,50},{j,-50,50}]]]

Plot the structure of sparse matrices:

m=ExampleData[{"Matrix","HB/west0381"},"Matrix"]

MatrixPlot[m]

OpenersExampleSection1(Spacer1)Scope  (19)

OpenersExampleSubsection0(Spacer1)Data  (10)

Plot a dense matrix:

m=Inverse@Table[Which[i==j,2.,i==j+1||i==j-1,-1.,True,0.],{i,100},{j,100}];

MatrixPlot[m]

Plot a sparse matrix:

m=Import["LinearAlgebraExamples/Data/dwg961b.cua"]

MatrixPlot[m]

Plot a nonrectangular "matrix", with missing entries transparent:

MatrixPlot[Table[Binomial[n,k],{n,0,25},{k,0,n}],Background->Lighter[Yellow]]

Negative entries are shown in cool colors, positive entries in warm colors, and zeros in white:

MatrixPlot[{{4,2,1},{3,0,-2},{0,0,-1}}]

Entries near zero are shown in a shade of gray; entries very close to zero may be shown in white:

MatrixPlot[{{1,2,3},{1.*^-16,0.00001,-0.00001},{-2,-1,-3}}]

Complex numbers are shown based on their real parts:

MatrixPlot[{{1,2,3},{-1+2I,-2+3I,-3-10I},{1+2I,2+3I,3-10I}}]

(RefLink[None, paclet:ref/None]) is interpreted as a missing value and displayed using transparency:

MatrixPlot[{{3,1,2},{-1,None,-2},{-1,-2,-3}},Background->Lighter[Yellow]]

Symbolic values other than (RefLink[None, paclet:ref/None]) are shown in dark red:

MatrixPlot[{{1,2,3},{a,0.00001,-0.00001},{-2,-1,-3}}]

Colors are shown darker for very sparse matrices to make entries more visible:

Row[{MatrixPlot[Table[1,{i,20},{j,20}]],MatrixPlot[SparseArray[{{10,10}->1},{20,20}]]}]

Show a matrix with irrational and arbitrary-precision entries:

MatrixPlot[{{-1.`100,2,Pi},{Sqrt[2],1/2,Catalan}}]

OpenersExampleSubsection1(Spacer1)Presentation  (9)

Add labels:

m=Block[{n=100},Table[Exp[i j 2Pi I/n],{i,0,n-1},{j,0,n-1}]];

MatrixPlot[m,FrameLabel->{i,j},PlotLabel->"Fourier Matrix"]

Give explicit color directives to specify colors for individual cells:

MatrixPlot[{{Red,Green,1,2},{Blue,Yellow,-1,-2}}]

Use a named color gradient:

MatrixPlot[Table[Cos[x y/150.],{x,-50,50},{y,-50,50}],ColorFunction->"Rainbow"]

Use a black-and-white color function to highlight the sparse structure of a matrix:

m=ExampleData[{"Matrix","WEST2021"},"Matrix"]

MatrixPlot[m,ColorFunction->"Monochrome"]

Use a custom color function with blue colors for negative values and red colors for positive values:

MatrixPlot[Table[Cos[x y/150.],{x,-50,50},{y,-50,50}],ColorFunctionScaling->False,ColorFunction->(Blend[{{-1,Blue},{0,White},{1,Red}},#]&)]

Use (RefLink[ColorRules, paclet:ref/ColorRules]) to color different values:

MatrixPlot[Table[Mod[i^2+j^2,3],{i,20},{j,20}],ColorRules->{0->Red,1->Green,2->Blue}]

Use both (RefLink[ColorRules, paclet:ref/ColorRules]) and (RefLink[ColorFunction, paclet:ref/ColorFunction]) to color elements, giving priority to (RefLink[ColorRules, paclet:ref/ColorRules]):

MatrixPlot[Table[Mod[i^2+j^2,5],{i,20},{j,20}],ColorRules->{0->Red,1->Green,2->Blue}]

Use (RefLink[Mesh, paclet:ref/Mesh]) and (RefLink[MeshStyle, paclet:ref/MeshStyle]) to provide an overlay mesh:

MatrixPlot[SparseArray[{Band[{3,1}]->-1,Band[{1,1}]->1,Band[{1,5}]->4},{10,10}],Mesh->All,MeshStyle->Directive[Gray,Dashed]]

Use a plot theme:

MatrixPlot[Table[Mod[i^2+j^2,5],{i,20},{j,20}],PlotTheme->"Marketing"]

OpenersExampleSection2(Spacer1)Options  (39)

OpenersExampleSubsection2(Spacer1)AspectRatio  (2)

Make all cells square:

MatrixPlot[Mod[Array[Binomial,{32,32},0],2],AspectRatio->Automatic]

Use a different aspect ratio:

MatrixPlot[Mod[Array[Binomial,{32,32},0],2],AspectRatio->1/2]

OpenersExampleSubsection3(Spacer1)Background  (2)

(RefLink[Background, paclet:ref/Background]) is normally visible only around the edges:

MatrixPlot[{{1,0,0,0.3},{1,1,0,0.3},{1,0,1,0.7}},Background->Lighter[Blue]]

The background "shows through" whenever an explicit entry is (RefLink[None, paclet:ref/None]):

MatrixPlot[{{1,0,0,None},{1,1,0,None},{1,None,1,0.7}},Background->Blue]

OpenersExampleSubsection4(Spacer1)ClippingStyle  (3)

By default, the clipped values are colored vibrant red and blue:

MatrixPlot[{{1,10,1},{100,0,1},{-10,0,-1}},PlotRange->{-1,1},ClippingStyle->Automatic]

Use explicit colors for the clipped values:

MatrixPlot[{{1,10,1},{100,0,1},{-10,0,-1}},PlotRange->{-1,1},ClippingStyle->{Green,Yellow}]

Use (RefLink[None, paclet:ref/None]) to indicate no style, showing the background in those cells:

MatrixPlot[{{1,10,1},{100,0,1},{-10,0,-1}},PlotRange->{-1,1},ClippingStyle->None,Background->LightBlue]

OpenersExampleSubsection5(Spacer1)ColorFunction  (5)

Use an explicit color function:

MatrixPlot[Table[x^2-y^2,{x,-10,10},{y,-10,10}],
ColorFunction->Hue]

Use a pure function as the color function:

MatrixPlot[Table[x^2-y^2,{x,-10,10},{y,-10,10}],ColorFunction->(RGBColor[1-#,#,1/2]&)]

Use a named color gradient from (RefLink[ColorData, paclet:ref/ColorData]):

MatrixPlot[Table[x^2-y^2,{x,-10,10},{y,-10,10}],ColorFunction->"TemperatureMap"]

If the color function is undefined for some value, then a dark red is substituted:

MatrixPlot[{{1,2},{a,4}},ColorFunction->Hue]

In this case, the color is defined for all the values:

MatrixPlot[{{1,2},{a,4}},ColorFunction->(If[#===a,Yellow,Hue[#]]&)]

For complex matrices, the real part is used for the color function:

MatrixPlot[{{1+I,2+I},{3+I,4}},ColorFunction->Hue]

MatrixPlot[{{1,2},{3,4}},ColorFunction->Hue]

OpenersExampleSubsection6(Spacer1)ColorFunctionScaling  (4)

By default, a nonlinear scaling of entries is used to differentiate values over a wide range:

MatrixPlot[Table[x^4-y^4,{x,-10,10},{y,-10,10}],ColorFunction->"TemperatureMap"]

With (RefLink[ColorFunctionScaling, paclet:ref/ColorFunctionScaling])->(RefLink[False, paclet:ref/False]), entries are not scaled:

MatrixPlot[Table[x^4-y^4,{x,-10,10},{y,-10,10}],ColorFunction->(ColorData["TemperatureMap"][Rescale[#,{-10^4,10^4}]]&),ColorFunctionScaling->False]

With (RefLink[ColorFunctionScaling, paclet:ref/ColorFunctionScaling])->(RefLink[False, paclet:ref/False]), (RefLink[MatrixPlot, paclet:ref/MatrixPlot]) behaves like (RefLink[ArrayPlot, paclet:ref/ArrayPlot]):

MatrixPlot[{{0,1,2}},ColorFunctionScaling->False]

ArrayPlot[{{0,1,2}}]

(RefLink[ColorFunctionScaling, paclet:ref/ColorFunctionScaling]) has no effect on (RefLink[ColorRules, paclet:ref/ColorRules]):

Table[MatrixPlot[{{0,1}},ColorRules->{0->Red,1->Green},ColorFunctionScaling->cf],{cf,{True,False}}]

OpenersExampleSubsection7(Spacer1)ColorRules  (6)

Specify color rules for explicit values or patterns:

MatrixPlot[Table[Mod[i^2+j^2,5],{i,30},{j,30}],ColorRules->{0->White,1->Red,2->Blue,3->Yellow,4->Green}]

Implement a "default color" by adding a rule for _:

MatrixPlot[Table[Mod[i^2+j^2,5],{i,30},{j,30}],ColorRules->{1->Red,2->Blue,3->Yellow,_->Gray}]

The array can contain symbolic values:

MatrixPlot[Transpose[Permutations[{a,b,c}]],ColorRules->{a->Red,b->Yellow,c->Blue},Mesh->True]

Use any patterns in (RefLink[ColorRules, paclet:ref/ColorRules]):

MatrixPlot[{{-1,0,1},{2,3,-5},{1,2,1}},ColorRules->{_?EvenQ->Red,1|-1->Blue,x_/;x<-1->Black}]

Rules are used in the order given:

MatrixPlot[{{1,0,1},{1,1,0}},ColorRules->{1->Red,0->Blue,1->Black}]

(RefLink[ColorRules, paclet:ref/ColorRules]) can be used together with (RefLink[ColorFunction, paclet:ref/ColorFunction]) and has higher priority:

MatrixPlot[Table[Mod[i^2+j^2,5],{i,30},{j,30}],ColorRules->{1->Red,2->Blue,3->Yellow},ColorFunction->(Gray&)]

OpenersExampleSubsection8(Spacer1)DataReversed  (1)

Reverse the order of columns:

MatrixPlot[Table[(1-x)^2+100(y-x^2)^2,{x,-2,2,0.03},{y,-2,2,0.03}],DataReversed->{False,True}]

OpenersExampleSubsection9(Spacer1)Frame  (1)

Do not use a frame:

MatrixPlot[Table[Sin[x^2+y^2],{x,-4,4,.04},{y,-4,4,.04}],Frame->False]

OpenersExampleSubsection10(Spacer1)FrameTicks  (5)

Do not include frame ticks:

MatrixPlot[Table[Exp[Sin[x]+Cos[y]^2],{x,-8,8,.4},{y,-8,8,.4}],FrameTicks->None]

Frame ticks at specific values:

MatrixPlot[Table[Exp[Sin[x]+Cos[y]^2],{x,-8,8,.4},{y,-8,8,.4}],FrameTicks->{{10,15,20,25},{12,24}}]

Frame ticks without a visible frame:

MatrixPlot[Table[Exp[Sin[x]+Cos[y]^2],{x,-8,8,.4},{y,-8,8,.4}],FrameStyle->Opacity[0],FrameTicksStyle->Opacity[1],FrameTicks->All]

With a mesh included, (RefLink[Frame, paclet:ref/Frame])->(RefLink[True, paclet:ref/True]) is required for frame ticks to be drawn:

MatrixPlot[Mod[Array[Binomial,{12,12},0],11],Mesh->True,Frame->True,FrameTicks->All]

Make the frame invisible:

MatrixPlot[Mod[Array[Binomial,{12,12},0],11],Mesh->True,Frame->True,FrameStyle->Opacity[0],FrameTicksStyle->Opacity[1],FrameTicks->All]

OpenersExampleSubsection11(Spacer1)MaxPlotPoints  (1)

By default, automatic methods are used to downsample large and/or sparse matrices:

sp=SparseArray[Table[{Random[Integer,{1,1000}],Random[Integer,{1,1000}]}->1,{3000}],{1000,1000}]

MatrixPlot[sp]

Without downsampling, the entries are less visible:

MatrixPlot[sp,MaxPlotPoints->Infinity]

Explicitly set downsampling values for (RefLink[MaxPlotPoints, paclet:ref/MaxPlotPoints]):

Table[MatrixPlot[sp,MaxPlotPoints->pp],{pp,{50,100,200}}]

The visual appearance in the resulting plot is also affected by the choice of (RefLink[ColorFunction, paclet:ref/ColorFunction]):

MatrixPlot[sp,ColorFunction->"Monochrome",MaxPlotPoints->Infinity]

OpenersExampleSubsection12(Spacer1)Mesh  (3)

Insert mesh lines between all cells:

MatrixPlot[SparseArray[{Band[{3,1}]->-1,Band[{1,1}]->1,Band[{1,5}]->4},{10,10}],Mesh->All]

Insert 19 row mesh lines and 1 column mesh line:

MatrixPlot[SparseArray[{Band[{3,1}]->-1,Band[{1,1}]->1,Band[{1,5}]->4},{10,10}],Mesh->{19,1}]

Use a sequence of colors for the mesh lines:

MatrixPlot[SparseArray[{Band[{3,1}]->-1,Band[{1,1}]->1,Band[{1,5}]->4},{10,10}],Mesh->{None,Table[{i,Hue[i/10]},{i,0,10}]}]

OpenersExampleSubsection13(Spacer1)MeshStyle  (1)

Make the mesh pink:

MatrixPlot[SparseArray[{Band[{3,1}]->-1,Band[{1,1}]->1,Band[{1,5}]->4},{10,10}],Mesh->All,MeshStyle->Pink]

OpenersExampleSubsection14(Spacer1)PlotRange  (3)

Plot all elements:

MatrixPlot[Table[1 /(2+Sin[x^2+y^2])-0.75,{x,-3,3,.05},{y,-3,3,.05}]]

Plot only elements with values from 0 to 1; clip the rest:

MatrixPlot[Table[1 /(2+Sin[x^2+y^2])-0.75,{x,-3,3,.05},{y,-3,3,.05}],PlotRange->1]

The first two entries in (RefLink[PlotRange, paclet:ref/PlotRange]) specify the range of rows and columns to include:

MatrixPlot[Table[1 /(2+Sin[x^2+y^2])-0.75,{x,-3,3,.05},{y,-3,3,.05}],PlotRange->{{20,60},All,All}]

OpenersExampleSubsection15(Spacer1)PlotTheme  (2)

Use a theme with simple ticks and grid lines in a bright color scheme:

MatrixPlot[Table[Mod[i^2+j^2,5],{i,20},{j,20}],PlotTheme->"Business"]

Turn off the grid lines:

MatrixPlot[Table[Mod[i^2+j^2,5],{i,20},{j,20}],PlotTheme->"Business",GridLines->None]

OpenersExampleSection3(Spacer1)Applications  (3)

Plot a sparse matrix:

sp=Import["LinearAlgebraExamples/Data/wm1.rra"]

MatrixPlot[sp]

Zoom in to the top-left diagonal block:

MatrixPlot[sp,PlotRange->{{1,60},{1,60}},ClippingStyle->{Red,Red}]

Plot the imaginary parts of a discrete Fourier transform matrix:

MatrixPlot[Im[Table[Exp[2. Pi I i j/32],{i,0,32},{j,0,32}]]]

Plot a table of values of five sine waves in random directions:

MatrixPlot[Table[Evaluate[Sum[Sin[RandomReal[10,2].{x,y}],{5}]],{x,0,5,.1},{y,0,5,.1}]]

OpenersExampleSection4(Spacer1)Properties & Relations  (5)

(RefLink[MatrixPlot, paclet:ref/MatrixPlot]) colors negative entries with cool colors and positive entries with warm colors:

MatrixPlot[{{1,2,3},{-1,0,-3},{4,0,-1}}]

(RefLink[ArrayPlot, paclet:ref/ArrayPlot]) uses gray scale:

ArrayPlot[{{1,2,3},{-1,0,-3},{4,0,-1}}]

(RefLink[MatrixPlot, paclet:ref/MatrixPlot]) rescales the matrix entries to differentiate values over a wide range:

MatrixPlot[{{100,2,3},{-1,0,-3},{4,0,-50}}]

Without rescaling, fewer elements can be differentiated:

MatrixPlot[{{50,2,3},{-1,0,-3},{4,0,-50}},ColorFunctionScaling->False]

Use (RefLink[ReliefPlot, paclet:ref/ReliefPlot]) for medical and geographic data:

ReliefPlot[Import["http://exampledata.wolfram.com/hailey.dem.gz","Data"],ColorFunction->"GreenBrownTerrain", MaxPlotPoints -> 200]

Use (RefLink[ListDensityPlot, paclet:ref/ListDensityPlot]) for structured or unstructured data sampled from continuous densities:

ListDensityPlot[Table[x+Sin[3x+y^2],{x,-3,3,0.1},{y,-3,3,0.1}],ColorFunction->"SunsetColors"]

Use (RefLink[GraphPlot, paclet:ref/GraphPlot]) for visualizing adjacency matrices:

GraphPlot[RandomChoice[{0.01,0.99}->{1,0},{100,100}]]

OpenersExampleSection5(Spacer1)Possible Issues  (2)

Using (RefLink[MaxPlotPoints, paclet:ref/MaxPlotPoints]) may result in artifacts not actually present in the original data:

sp=SparseArray[{{i_,j_}/;Mod[i-j,3]==0->1},{40,40}];

MatrixPlot[sp]

With a small (RefLink[MaxPlotPoints, paclet:ref/MaxPlotPoints]) option value, all entries become nonzero:

MatrixPlot[sp,MaxPlotPoints->20]

Entries very close to zero may be treated as zero:

MatrixPlot[{{10,2,3},{4,1,5},{6,-10,1.*^-100}}]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

Plot the (RefLink[Sin, paclet:ref/Sin]) function at integer points:

MatrixPlot[Table[Sin[x y/100],{x,-50,50},{y,-50,50}]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ArrayPlot, paclet:ref/ArrayPlot])  ▪  (RefLink[ListDensityPlot, paclet:ref/ListDensityPlot])  ▪  (RefLink[ReliefPlot, paclet:ref/ReliefPlot])  ▪  (RefLink[Grid, paclet:ref/Grid])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Matrices and Linear Algebra, paclet:guide/MatricesAndLinearAlgebra])}, {▪(RefLinkPlain[Data Visualization, paclet:guide/DataVisualization])}, {▪(RefLinkPlain[Graphs and Matrices, paclet:guide/GraphsAndMatrices])}})}})