Thanks to MatrixPlot, I can observe some patterns in a matrix. However, when I use MatrixPlot :

MatrixPlot[A, ColorFunction -> "Monochrome"]

The matrix is colored with 3 colors : - black - white - brown

Is it possible to color the matrix only with 2 colors : - black when there is something not null - white when the element is null ?


1 Answer 1

dat = RandomChoice[{Null, 0, 5}, {3, 3}];
MatrixPlot[dat, ColorFunction -> "Monochrome"]

Mathematica graphics

You can replace Null with None:

None is interpreted as a missing value and displayed using transparency

MatrixPlot[dat /. Null -> None, ColorFunction -> "Monochrome"]

Or, use ColorRules to display Null as White:

MatrixPlot[dat, ColorFunction -> "Monochrome", ColorRules -> {Null -> White}]

Alternatively, use ColorRules to specify the colors directly,

MatrixPlot[dat, ColorRules -> {Null | 0 -> White, _ -> Black}]

All three approaches above give:

Mathematica graphics

To display Null as White and all other values as Black, use

MatrixPlot[dat, ColorRules -> {Null -> White, _ -> Black}]

Mathematica graphics


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.