3
$\begingroup$

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!

$\endgroup$

3 Answers 3

4
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Vaggelis_Z
    Mar 25, 2015 at 14:17
  • $\begingroup$ 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.) $\endgroup$ Mar 25, 2015 at 15:27
  • $\begingroup$ 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. $\endgroup$ Mar 25, 2015 at 15:54
  • $\begingroup$ So 1 row and 2 columns requires aspect ratio of 0? Does not compute :-) $\endgroup$
    – LLlAMnYP
    Mar 27, 2015 at 17:21
  • $\begingroup$ Should have written n >1 as well. $\endgroup$ Mar 27, 2015 at 20:04
5
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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!

$\endgroup$
3
  • $\begingroup$ Note that it didn't remove the vertical white space. But see my answer. $\endgroup$ Mar 25, 2015 at 13:16
  • $\begingroup$ @Sjoerd C. de Vries, Do you mean the space above and below the image? $\endgroup$ Mar 25, 2015 at 13:19
  • $\begingroup$ Yes, that's what I meant and that's what the OP painted red in his figure. $\endgroup$ Mar 25, 2015 at 13:19

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.