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]

correct output


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

unexpected output

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

  • 1
    $\begingroup$ ArrayPlot[] exhibits the same problem, apparently. $\endgroup$ – J. M. is in limbo Apr 13 '16 at 21:19
  • $\begingroup$ Should I file a bug report? What do you think. $\endgroup$ – 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:

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

and use in MatrixPlot:

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

Mathematica graphics

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]

Mathematica graphics

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

Mathematica graphics

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]

Mathematica graphics

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

  • $\begingroup$ Also, look at this weirdness: ListDensityPlot[ConstantArray[0, {#, #}], ColorFunctionScaling -> False, ColorFunction -> GrayLevel] & /@ {2, 3, 4, 5} $\endgroup$ – Jason B. Apr 13 '16 at 11:47
  • $\begingroup$ @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. $\endgroup$ – rcollyer Apr 13 '16 at 13:29
  • $\begingroup$ @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) $\endgroup$ – Jason B. Apr 13 '16 at 13:31
  • 1
    $\begingroup$ @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 ... $\endgroup$ – rcollyer Apr 13 '16 at 13:32
  • $\begingroup$ @JasonB I don't, Ilian might. $\endgroup$ – rcollyer Apr 13 '16 at 13:40

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