4
$\begingroup$

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 ?

$\endgroup$
2
  • $\begingroup$ Why not use ArrayPlot[] instead? $\endgroup$ Feb 27, 2016 at 23:35
  • $\begingroup$ Related: (6615) $\endgroup$
    – Mr.Wizard
    Feb 28, 2016 at 16:20

1 Answer 1

4
$\begingroup$
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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