# How can I build a GraphicsGrid of fixed size that rearranges the empty plots?

OK, let's make one final attempt; they say the third time is the lucky one! Following @rm-rf 's suggestions, I will try to be as clear as I can.

My Mathematica code reads and plots simultaneously some data files. The output of the function plo below can be either a Graphics object (ListPlot) or a null string ("") if the data it reads doesn't exist. I want to arrange the 20 plots below in a grid of fixed dimensions ($4 \times 4 \times 4 \times 4 \times 2 \times 2$) like so:

P0 = Show[GraphicsGrid[{
{plo[1, 1], plo[2, 1], plo[4, 1], plo[7, 1]},
{plo[1, 2], plo[3, 1], plo[5, 1], plo[7, 2]},
{plo[1, 3], plo[3, 2], plo[6, 1], plo[8, 1]},
{plo[1, 4], plo[3, 3], plo[6, 2], plo[8, 2]},
{plo[1, 5], plo[3, 4]},
{plo[1, 6], plo[3, 5]}}]]


Since some data files may not exist, I would like the plots to rotate so that there are no blank spaces in between the plots but all the Null plots get moved to the end of the grid. For example, if plo[2, 1], plo[7, 2] are missing I would like them to take up the last two slots in my grid while the rest of the plots preserve their ordering.

Ok, here goes (again). (Probably) replacing this line:

P0 = Show[GraphicsGrid[{
{plo[1, 1], plo[2, 1], plo[4, 1], plo[7, 1]},
{plo[1, 2], plo[3, 1], plo[5, 1], plo[7, 2]},
{plo[1, 3], plo[3, 2], plo[6, 1], plo[8, 1]},
{plo[1, 4], plo[3, 3], plo[6, 2], plo[8, 2]},
{plo[1, 5], plo[3, 4]},
{plo[1, 6], plo[3, 5]}}


with this

Module[{list, newlist},
list=Select[Flatten[{
{plo[1, 1], plo[2, 1], plo[4, 1], plo[7, 1]},
{plo[1, 2], plo[3, 1], plo[5, 1], plo[7, 2]},
{plo[1, 3], plo[3, 2], plo[6, 1], plo[8, 1]},
{plo[1, 4], plo[3, 3], plo[6, 2], plo[8, 2]},
{plo[1, 5], plo[3, 4]},
{plo[1, 6], plo[3, 5]}}],! StringQ@#&];
GraphicsGrid[{newlist[[1;;4]],newlist[[5;;8]],newlist[[9;;12]],newlist[[13;;16]],newlist[[17;;18]],newlist[[19;;20]]}]
]


will work

• Yes it's working!!! If a plot is missing does it replace it with the first next available plot or it reconfigures the grid randomly? Commented Aug 1, 2013 at 16:43
• it reconfigures the grid so that the shape of the grid is as close to a square as possible (adding blank plots at the end)
– gpap
Commented Aug 1, 2013 at 16:55
• I found a drawback. If all files are present then it produces a 5x4 grid not the standard 4x4, 2x2. Why? Commented Aug 1, 2013 at 16:56
• Is it possible to keep the 4x4, 2x2 structure and fill the blank slots with the next available plot? It does not matter to me if the shape of the grid is square or not; but the position of the plots are important. Commented Aug 1, 2013 at 17:05
• I just edited it to do that but surely you know how to rearrange a list yourself. I am really not going to deal with this any further.
– gpap
Commented Aug 1, 2013 at 17:11