# Labeling elements of a grid

Let's create a simple $3 \times 3$ grid of images

img = ExampleData[{"TestImage", "Lena"}];
P0 = Show[GraphicsGrid[{{img, img, img}, {img, img, img}, {img, img, img}}]]


My question: Is there a quick and elegant way of adding labels (1), (2), ... , (9) at the upper right part of each image? The labels should be located outside the upper right part of each panel, like the following

EDIT

Obviously, the above is just a simple case. In my real situation I have a $4 \times 4$ grid containing 16 different images with different names. So the solution must not work only for the same image, repeated many times.

EDIT II

If I try

im0 = Import["Lena.jpg"];
imn0 = Labeled[im0, {Style["(1)", FontFamily -> "Helvetica", 12, Bold]}, {{Top, Right}}];
plot0 = Show[GraphicsGrid[{{imn0, imn0, imn0, imn0}, {imn0, imn0, imn0,
imn0}, {imn0, imn0, imn0, imn0}, {imn0, imn0, imn0, imn0}}]]


then I get

Why the labels are not shown properly?

Any suggestions?

## 2 Answers

GraphicsGrid[i = 0;
Map[Show[#,
Graphics[
Inset[i++; Style[Text["(" <> ToString[i] <> ")"], 18, White],
Scaled[{.15, .9}]]]] &] /@ Table[img, {3}, {3}],
ImageSize -> 300]


Or maybe this is your after?

GraphicsGrid[i = 0;Map[Labeled[#, i++;
Style[Text["(" <> ToString[i] <> ")"],
Black], {{Right, Top}}] &] /@ Table[img, {3}, {3}]]


As your edit,maybe

GraphicsGrid[i = 0; Map[Labeled[#, i++;
Style[Text["(" <> ToString[i] <> ")"], Black], {{Top, Right}},
ImageSize -> 100] &] /@ Table[img, {3}, {3}]]


• Nice! But as I explained the labels should be outside the image and at the upper right (not left) side. – Vaggelis_Z May 22 '17 at 8:47
• Not exactly! The labels in your second approach are located next to the images, while I want them just above the upper right corner. – Vaggelis_Z May 22 '17 at 9:13
• Well,English kill me. :) – yode May 22 '17 at 9:16
• Please, see my edit. – Vaggelis_Z May 22 '17 at 9:32
• Yes, the third approach is just what I need! A last issue: what if the grid contain nine different images instead of the same img? – Vaggelis_Z May 22 '17 at 10:45

In the following, I create a new graphics with PlotRange 0 to 1.2. I then use Inset to insert the image into the new graphics and specify that it should occupy the plot range 0 to 1. This means that I have a padding on the upper and right-hand side of the image of 0.2 plot range. That gives me space to put the text in.

img = ExampleData[{"TestImage", "Lena"}];

labeled[img_, label_] := Graphics[{
Inset[img, {0, 0}, {0, 0}, {1, 1}],
Text[Style[label, FontSize -> 12], {1.1, 1.1}]
}, PlotRange -> {{0, 1.2}, {0, 1.2}}]

imgs = MapThread[
labeled, {ConstantArray[img, 9], StringTemplate["()"] /@ Range[9]}
];

GraphicsGrid@Partition[imgs, 3]


• Is there a way to insert the labels exactly at the upper right corner of each panel? In your approach the labels should be moved a little bit left. – Vaggelis_Z May 22 '17 at 9:06
• @Vaggelis_Z That question indicates to me that you have not taken the time to properly understand the answer. – C. E. May 22 '17 at 9:07
• Two more issues: (1). In my real situation I have nine different images, while your method works for only one image, repeated nine times, (2) I should be able to control the fonts of the labels. – Vaggelis_Z May 22 '17 at 9:11
• @Vaggelis_Z The images have different sizes or what? Provide a better example then. Why do you say that you are not able to control the fonts and the position of the labels? It makes no sense. – C. E. May 22 '17 at 9:20
• Please see my edit. All images have the same size but different names. – Vaggelis_Z May 22 '17 at 9:31