6

Can I write text, or add a specific text label to individual squares of an ArrayPlot[...] graphic? Specifically, I would like to assign an two-dimensional integer coordinate label to each square.

Roger Harris
  • 1,497
  • 1
  • 12
  • 20

4 Answers4

4

There might be an easier way, but what about this:

array = RandomInteger[100, {10, 10}];
arrayPlot = ArrayPlot[array, ImageSize -> 500, PlotRangePadding -> 0]

grid 1

numbers = 
 Graphics[{White, 
   Table[Text[ 
      StringJoin[ToString /@ {n , ", ", m}], 
   {n - 0.5, m - 0.5}], 
   {n, 1, 10}, {m, 1, 10}]}]

Show[arrayPlot, numbers]

grid 2

cormullion
  • 24,243
  • 4
  • 64
  • 133
4

Coordinates in the form needed by Part:

data = RandomReal[1, {5, 7}];

{x, y} = Dimensions @ data;

f[x_, y_, shift_] := Array[Text[{x - #2 + 1, #}, {##} - shift] &, {y, x}]

ArrayPlot[data, Epilog -> {Black, f[x, y, 0.49], White, f[x, y, 0.51]}]

Mathematica graphics

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
2

Using MapIndexed:

SeedRandom[1];
array = RandomInteger[100, {10, 10}];
labels = MapIndexed[
   Text[Style[
      StringTrim[ToString[#2], {"{", "}"}]
      , 10, GrayLevel[RandomReal[]]
      ]
     , #2 - 0.5] & (*end of text*)
   , array, {2}];
arrayPlot = ArrayPlot[array
  , ImageSize -> 500
  , PlotRangePadding -> 0
  , Axes -> True
  , Epilog -> { labels
    }
  ]

enter image description here

Syed
  • 52,495
  • 4
  • 30
  • 85
1
SeedRandom[1];
data = RandomReal[1, {5, 7}];

labels = MapIndexed[Text[
    Style[Row[{1 + Dimensions[data][[1]] - #2, #} & @@ #2, ","], 
      16, If[.5 > #, Black, White]], 
    #2 - .5] &, 
  Reverse /@ Transpose[data], {2}]; 

ArrayPlot[data, Epilog -> labels, FrameTicks -> All]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896