3

Let's see a simple example

s0 = Plot[Sin[x], {x, -10, 10}, Frame -> True, FrameLabel -> {"x", "y"}, 
       ImageSize -> 550];
E0 = Export["test.jpg", s0];
img = Import["test.jpg"];

S1 = GraphicsGrid[{{img, img}, {img, img}}]

and here is the output

enter image description here

Is there a way to delete the white areas that I marked in the GraphicsGrid result with red boxes?

Many thanks in advance!

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
Vaggelis_Z
  • 8,740
  • 6
  • 34
  • 79

3 Answers3

5

This is an incredibly annoying issue that led me to write my own package at one point. While we're dealing with a grid of images of equal sizes, this is easily solvable (see answers of Sjoerd C. de Vries and Alexei Boulbitch).

Unfortunately, we (myself and the topic-starter, at least) often want GraphicsGrid to behave more like Grid.

Compare, for example, the very different outputs of Grid and GraphicsGrid in the following example:

s0 = Plot[Sin[x], {x, -10, 10}, Frame -> True, FrameLabel -> {"x", "y"},
AspectRatio -> Full, ImageSize -> {550, 300}];
s00 = Plot[Sin[x], {x, -10, 10}, Frame -> True, FrameLabel -> {"x", "y"},
AspectRatio -> Full, ImageSize -> {550, 550}];
GraphicsGrid[{{s0, s0}, {s00, s00}}, Spacings -> 0]
Grid[{{s0, s0}, {s00, s00}}]

So I wrote my own function to arrange plots into grids. I'm afraid, there's far too much code (and bad code, at that) to include in one short answer, but here's the general approach I take when I want to be the one deciding how and where to place Graphics:

img := Plot[Sin[x], {x, 0, 10}, 
  ImageSize -> RandomInteger[{200, 400}, 2], AspectRatio -> Full, 
  Frame -> True]
img1 = img; img2 = img; img3 = img; img4 = img;
grid = {{img1, img2}, {img3, img4}};
sizes = Map[ImageDimensions, grid, {2}]
    (* {{{315, 229}, {335, 234}}, {{382, 268}, {373, 390}}} *)
rowheights = Max /@ sizes[[All, All, 2]]
    (* {234, 390} *)
colwidths = Max /@ Transpose@sizes[[All, All, 1]]
    (* {382, 373} *)
positions = Position[grid, _Graphics]
    (* {{1, 1}, {1, 2}, {2, 1}, {2, 2}} *)

Having made all these definitions we then gather a tightly packed collection of plots using a bunch of Insets. Here the space allocated to each plot is the width of the widest element of the column and the height of the heighest element of the row. Each element is aligned to the bottom-right. This is tweakable with the settings of ImageScaled and addition/subtraction of the appropriate element of rowheight/colwidth.

Graphics[Table[
  Inset[grid[[Sequence @@ index]], {Plus @@ 
     colwidths[[;; index[[2]]]], 
    Plus @@ rowheights - Plus @@ rowheights[[;; index[[1]]]]}, 
   ImageScaled[{1, 0}]], {index, positions}], 
 ImageSize -> {Plus @@ colwidths, Plus @@ rowheights}, 
 ImagePadding -> None, 
 PlotRange -> {{0, Plus @@ colwidths}, {0, Plus @@ rowheights}}, 
 AspectRatio -> Plus @@ rowheights/Plus @@ colwidths, 
 PlotRangePadding -> None]

This returns

Tightly packed

as compared to normal GraphicsGrid output

Normal

Edit I have a sneaking suspicion, that the spaces automatically spliced in where I use shorthand @@ for Apply might break the code and make MMA try to divide a List by Plus, if that happens, apply parentheses generously.

LLlAMnYP
  • 11,486
  • 26
  • 65
4

The trick is to give the GraphicsGrid the same aspect ratio as the tightly packed collection of graphics would have.

GraphicsGrid[{{img, img}, {img, img}}, 
   Spacings -> 0,  
   AspectRatio -> 1/GoldenRatio
]

Mathematica graphics

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
  • It works fine! But what about when we have 3x3 gird {{img, img}, {img, img},{img, img}}? The Golden Ratio deletes all the vertical white area but a lot of horizontal white areas appear. – Vaggelis_Z Mar 25 '15 at 14:17
  • Use (n-1)/GoldenRatio, with n the number of image rows (of two images). It's not quite what I would say intuitively but it works (of course, this works only for images that start at an aspect ratio of 1/GoldenRatio. If not, modify correspondingly.) – Sjoerd C. de Vries Mar 25 '15 at 15:27
  • Actually with n rows and m columns with m>1 it should be (n-1)/(m-1) / GoldenRatio for reasons not entirely clear to me. – Sjoerd C. de Vries Mar 25 '15 at 15:54
  • So 1 row and 2 columns requires aspect ratio of 0? Does not compute :-) – LLlAMnYP Mar 27 '15 at 17:21
  • Should have written n >1 as well. – Sjoerd C. de Vries Mar 27 '15 at 20:04
0

Try this:

    s0 = Plot[Sin[x], {x, -10, 10}, Frame -> True, 
   FrameLabel -> {"x", "y"}, ImageSize -> 550];
E0 = Export["test.jpg", s0];
img = Import["test.jpg"];

S1 = GraphicsGrid[{{img, img}, {img, img}}, Spacings -> {10, -120}]

which should look like the following:

enter image description here

Play with the values of the Spacings. Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96