2
$\begingroup$

I want to understand why the order of the text is reversed from what it should be? If I compare the first and second results, I see that the {1,1}, {2,2},... diagonal is going down the wrong diagonal. How could I alter this, such that the order is correct? For reference, here is the original question...ArrayPlot with cells labeled by corresponding values

SeedRandom[41, Method -> "Legacy"];(*for reproducibility*)  
mat = RandomReal[1, {4, 4}]  
ArrayPlot[  
 mat,  
 Epilog -> {Red, MapIndexed[
   Text[#1, Reverse[#2 - 1/2]] &, mat, {2}]},  
 Mesh -> True, ImageSize -> 400]  

mat = RandomReal[1, {4, 4}]  
ArrayPlot[  
 mat,  
 Epilog -> {Red, MapIndexed[
   Text[#2, Reverse[#2 - 1/2]] &, mat, {2}]},  
 Mesh -> True, ImageSize -> 400]
$\endgroup$
6
  • $\begingroup$ Your code doesn't work. $\endgroup$
    – C. E.
    Commented Oct 4, 2014 at 23:39
  • $\begingroup$ @Pickett code has been updated, had a rogue Style command that I removed. Thanks! $\endgroup$
    – tarhawk
    Commented Oct 4, 2014 at 23:50
  • $\begingroup$ I think you forgot to include the Reverse. Reread J.M.'s answer, he uses Reverse[mat]. $\endgroup$ Commented Oct 5, 2014 at 0:10
  • $\begingroup$ @DumpsterDoofus yes, you are correct. Thanks for the extra set of eyes. Lol, I feel sheepish now! $\endgroup$
    – tarhawk
    Commented Oct 5, 2014 at 0:17
  • $\begingroup$ @tarhawk: It still doesn't fix your second example, but it's a step in the right direction. $\endgroup$ Commented Oct 5, 2014 at 0:21

1 Answer 1

3
$\begingroup$
SeedRandom[41, Method -> "Legacy"];

(mat = RandomReal[1, {4, 4}]) // TableForm

enter image description here

 ArrayPlot[
     mat,
     Epilog -> 
      MapIndexed[Text[Framed[#1, Background -> White], #2 - 1/2] &,
        Transpose@Reverse@mat, {2}],
     Mesh -> True,
     PlotTheme -> "Detailed",
     ImageSize -> 400]

enter image description here

Update

To handle negative values you must specify a "matching" PlotRange

SeedRandom[41, Method -> "Legacy"];
mat = Round[RandomReal[{-1, 1}, {4, 4}], 0.01];

ArrayPlot[
 mat,
 ColorFunction -> "RedGreenSplit",
 Epilog -> 
  MapIndexed[Text[Framed[#1, Background -> White], #2 - 1/2] &,
   Transpose@Reverse@mat, {2}],
 Mesh -> True,
 PlotRange -> {-1, 1},
 PlotTheme -> "Detailed",
 ImageSize -> 400]

enter image description here

$\endgroup$
3
  • $\begingroup$ I believe that you the first argument of ArrayPlot should be mat as your values do not correspond to correct shades $\endgroup$
    – ubpdqn
    Commented Oct 5, 2014 at 12:27
  • $\begingroup$ @ubpdqn Thank you very much - updated :) $\endgroup$
    – eldo
    Commented Oct 5, 2014 at 12:38
  • $\begingroup$ @eldo The additional transpose also did the trick for the ordering. $\endgroup$
    – tarhawk
    Commented Oct 5, 2014 at 13:54

Your Answer

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

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