6
$\begingroup$

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.

$\endgroup$
2

4 Answers 4

4
$\begingroup$

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

$\endgroup$
4
$\begingroup$

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

$\endgroup$
1
  • 1
    $\begingroup$ Clever text styling! Embossing by the Boss. $\endgroup$
    – cormullion
    Jan 8, 2013 at 11:39
2
$\begingroup$

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

$\endgroup$
1
$\begingroup$
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

$\endgroup$
1
  • $\begingroup$ It is much more readable. Thanks for the text coloring tip. $\endgroup$
    – Syed
    Sep 5, 2023 at 5:05

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.