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}}]]

enter image description here

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

enter image description here


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.


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

enter image description here

Why the labels are not shown properly?

Any suggestions?

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

Mathematica graphics

Or maybe this is your after?

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

Mathematica graphics

As your edit,maybe

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

Mathematica graphics

  • $\begingroup$ Nice! But as I explained the labels should be outside the image and at the upper right (not left) side. $\endgroup$ – Vaggelis_Z May 22 '17 at 8:47
  • $\begingroup$ Not exactly! The labels in your second approach are located next to the images, while I want them just above the upper right corner. $\endgroup$ – Vaggelis_Z May 22 '17 at 9:13
  • $\begingroup$ Well,English kill me. :) $\endgroup$ – yode May 22 '17 at 9:16
  • $\begingroup$ Please, see my edit. $\endgroup$ – Vaggelis_Z May 22 '17 at 9:32
  • $\begingroup$ 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? $\endgroup$ – 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]

Mathematica graphics

  • $\begingroup$ 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. $\endgroup$ – Vaggelis_Z May 22 '17 at 9:06
  • $\begingroup$ @Vaggelis_Z That question indicates to me that you have not taken the time to properly understand the answer. $\endgroup$ – C. E. May 22 '17 at 9:07
  • $\begingroup$ 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. $\endgroup$ – Vaggelis_Z May 22 '17 at 9:11
  • $\begingroup$ @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. $\endgroup$ – C. E. May 22 '17 at 9:20
  • $\begingroup$ Please see my edit. All images have the same size but different names. $\endgroup$ – Vaggelis_Z May 22 '17 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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