# MatrixPlot ignores ColorFunction for 0 matrices [duplicate]

I noticed that when plotting a matrix with custom color function there seems to be a special behavior for matrices with just 0's. In this case the plot is simply white. If there is one non-zero everything works correctly. Any ideas?

MatrixPlot[{{0, 0.5}}, ColorFunctionScaling -> False, ColorFunction -> GrayLevel]


but

MatrixPlot[{{0, 0}}, ColorFunctionScaling -> False, ColorFunction -> GrayLevel]


where we would expect a black cells. (Mathematica 10.0.2 Linux)

## marked as duplicate by MarcoB, user9660, Edmund, m_goldberg, Karsten 7.May 27 '16 at 11:58

• ArrayPlot[] exhibits the same problem, apparently. – J. M. is away Apr 13 '16 at 21:19
• Should I file a bug report? What do you think. – flexman Apr 14 '16 at 12:58

Yeah, I see the same behavior on MMA 10.4: something funny is going on here. This is really an extended comment rather than an answer.

You could define your own graylevel function that also prints the values passed to it for evaluation, to check what is going on:

Clear[gray]
gray[x_] := Module[{}, Print[x]; RGBColor[x, x, x]]


and use in MatrixPlot:

MatrixPlot[{{0, 0.5}}, ColorFunctionScaling -> False, ColorFunction -> gray]


As you can see the correct values seem to be passed to our color function, and interpreted correctly.

Normally MatrixPlot would send each value to be converted into color to the colorfunction separately:

MatrixPlot[{{0.2, 0.2}}, ColorFunctionScaling -> False, ColorFunction -> gray]


But when those values are all zeros, then only one single value is sent to the colorfunction:

and the incorrectly colored plot is returned.

Even in cases in which the correct plot is returned, the zero values are conflated into one call to the colorfunction:

MatrixPlot[{{0, 0}, {0, 0.5}}, ColorFunctionScaling -> False, ColorFunction -> gray]


Perhaps MatrixPlot is being too clever here and applying some kind of internal optimization?

• Also, look at this weirdness: ListDensityPlot[ConstantArray[0, {#, #}], ColorFunctionScaling -> False, ColorFunction -> GrayLevel] & /@ {2, 3, 4, 5} – Jason B. Apr 13 '16 at 11:47
• @JasonB easy. The list plots accept data in two forms: array and point-value form. For ListDensityPlot, the point-value form is {{x, y, z} ..} which is exactly what you're sending it with ConstantArray[0, {3, 3}]. And, you're sending it 3 points at the same location, so it really can't do anything sensible. – rcollyer Apr 13 '16 at 13:29
• @rcollyer You mean it can't tell the difference between a $3\times3$ array and a list of 3 $\{x, y, z\}$ values? Unbelievable :-D (also, I feel dumb on that one lol) – Jason B. Apr 13 '16 at 13:31
• @JasonB I know, shocking! (well at least about it's limitations). Don't feel dumb, this hits many people. It's even more confusing when your working with a $n\times 3$ pencil ... – rcollyer Apr 13 '16 at 13:32
• @JasonB I don't, Ilian might. – rcollyer Apr 13 '16 at 13:40