Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a rectangular matrix, which is m-by-10. Each element is either 0 or 1. I would like 0 to correspond to white elements, and 1 to red elements. This works fine if m = 100 or similar small values:

m = 100;
list = Table[Table[RandomInteger[], {j, 1, 10}], {i, 1, m}];
Tally[Flatten[list]]
MatrixPlot[list, FrameTicks -> None, ImageSize -> {300, 300}, 
 ColorRules -> {0 -> White, 1 -> Red}]

MP1

However, if I choose m = 200, the coloring becomes strange and no longer only white or red:

m = 200;
list = Table[Table[RandomInteger[], {j, 1, 10}], {i, 1, m}];
Tally[Flatten[list]]
MatrixPlot[list, FrameTicks -> None, ImageSize -> {300, 300}, 
 ColorRules -> {0 -> White, 1 -> Red}]

MP2

Why do I see orange if each element is either 0 or 1? Is there any way I can correct this so that the only colors that appear are white or red?

Setting ColorFunctionScaling -> False does not seem to correct the problem:

MP3

share|improve this question
2  
Did you try with ArrayPlot? –  belisarius Jun 9 '12 at 16:59
add comment

2 Answers 2

up vote 16 down vote accepted

Seems that the automatic setting of MaxPlotPoints is too low. You can set it to something high (or even Infinity) to get around this.

m = 200;
list = Table[Table[RandomInteger[], {j, 1, 10}], {i, 1, m}];
Tally[Flatten[list]]
MatrixPlot[list, FrameTicks -> None, ImageSize -> {300, 300}, 
 ColorRules -> {0 -> White, 1 -> Red}, MaxPlotPoints -> \[Infinity]]
share|improve this answer
3  
From the docs: "With the default setting MaxPlotPoints -> Automatic, sufficiently large or sparse matrices are downsampled so that their structure is visible in the plot generated by MatrixPlot." This seems to be what's happening here, and Mathematica is suitably interpolating colors to go along with this downsampling. –  J. M. Jun 9 '12 at 17:14
1  
Looks like MaxPlotPoints needs to only be Max@Dimensions@list, although no harm with using ∞... –  rm -rf Jun 9 '12 at 17:47
1  
Thanks. Although, from reading the documentation, it is not clear to me why downsampling by altering the colors would give a more visible structure. So I don't really understand why Mathematica does this by default. –  Andrew Jun 9 '12 at 18:16
add comment

As belisarius commented you can use ArrayPlot, which does not compress the range of the data.

list = RandomInteger[1, {10, 300}];

ArrayPlot[list, ColorRules -> {0 -> White, 1 -> Red}, 
 PlotRangePadding -> 0, ImageSize -> 600]

Mathematica graphics

Perhaps better in this case you can also build the image raster directly:

Image[ list /. {0 -> {1, 1, 1}, 1 -> {1, 0, 0}} ]

Mathematica graphics

share|improve this answer
3  
As an aside note, I think these beasts are safer when framed. Sometimes there is a deceiving lot of white space at the borders. –  belisarius Jun 10 '12 at 8:57
    
@belisarius in your opinion is throwing Framed on the second option sufficient? –  Mr.Wizard Jun 10 '12 at 8:59
    
Thank you! Why is it preferable to use Image rather than ArrayPlot? –  Andrew Jun 10 '12 at 14:14
2  
@Andrew it may render and resize faster than the object produced by ArrayPlot, if you don't need the added features such as the frame that belisarius mentioned. It can also be quite flexible as you work with the {Red, Green, Blue} values directly (or optionally another colorspace). –  Mr.Wizard Jun 10 '12 at 14:47
    
@Mr.Wizard Yes. Anything showing a hint of where the "actual" borders are would do. My comment was not about asking you to modify the code, but to warn the OP about taking care of it. +1, BTW –  belisarius Jun 10 '12 at 16:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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