# How can I build a GraphicsGrid of arbitrary size?

This is actually a very simple scenario, in order to make simpler this question Part I.

Consider the following piece of Mathematica code

data1 = {{-1, 2}, {3, 1}};
data2 = {{1, -5}, {2, -1}};
data3 = {{0, 1}, {-3, 4}};
data4 = {{-4, 9}, {8, 10}};
data5 = {{-2, 5}, {-2, -5}};
data6 = {{-5, -3}, {0, 1}};
data7 = {{-1, -2}, {-1, 1}};
data8 = {{2, 9}, {7, 1}};

L1 = ListPlot[data1];
L2 = ListPlot[data2];
L3 = ListPlot[data3];
L4 = ListPlot[data4];
L5 = ListPlot[data5];
L6 = ListPlot[data6];
L7 = ListPlot[data7];
L8 = ListPlot[data8];

P0 = GraphicsGrid[{{L1, L2, L3}, {L4, L5, L6}, {L7, L8}}, ImageSize -> 550]


The simple two elements lists could be massive external data files. So, my question is: If I don't know the exact number of data files beforehand (in my case for example, it could be anything between 0 and 8), how can I define the GraphicsGrid? If a list (file) is missing then I will get an empty slot in the grid. Is it possible to fit the empty slot with the next available item, but maintaining the grids's original structure (3 rows, the first 2 with 3 plots per row and the last one with only 2 plots)?

EDIT

I see that the use of lists is not helpful because it does not match to the actual case. So, let me explain the situation: I run a FORTRAN program which produces 8 data files. However, there are cases in which the number of produced files is smaller than 8, even zero but never larger than 8. So in order to plot these files I constructed this code. However, many complained about this and for this reason let's suppose the following:

SetDirectory[];

data1 = Import["data1.ab","Table"];
data2 = Import["data2.cd","Table"];
data3 = Import["data3.ef","Table"];
data4 = Import["data4.gh","Table"];
data5 = Import["data5.ij","Table"];
data6 = Import["data6.kl","Table"];
data7 = Import["data7.mn","Table"];
data8 = Import["data8.op","Table"];

L1 = ListPlot[data1];
L2 = ListPlot[data2];
L3 = ListPlot[data3];
L4 = ListPlot[data4];
L5 = ListPlot[data5];
L6 = ListPlot[data6];
L7 = ListPlot[data7];
L8 = ListPlot[data8];

P0 = GraphicsGrid[{{L1, L2, L3}, {L4, L5, L6}, {L7, L8}}, ImageSize -> 550]


The structure of the grid corresponds to the case where all 8 files are present. What I want is an automatic procedure reading every available data file, determine which of them are present (not empty) and then plot them in a grid.

• Why did you not update the first part of your question? The MWE is what we advocated, but leaving the old one in disarray is not so cool. – Yves Klett Aug 1 '13 at 9:38
• @YvesKlett Do you see know how difficult is to replicate the exact conditions using a simple example? Yes, I am getting answers but neither of them fit to what I really want simply because simplifying the scenario all the details are lost. I cannot understand why my original post was deleted ... – Vaggelis_Z Aug 1 '13 at 11:39
• There were a lot of extraneous details, as was pointed out by several users. Even after so many requests for more information, etc., where did you ever specify (before this comment) that 8 files corresponds to max. 20 plots? So far everyone (or maybe just me) has been assuming n files -> n plots. Where have you mentioned that you want the 20 plots as 4x4 and 2x2? "How hard can it be" — well, it's not that hard, but you're not making it easier... If you had spent 30 minutes improving/simplifying your MWE, you would've saved 9 hours of haggling with the commenters. Just sayin'... :) – rm -rf Aug 1 '13 at 14:13
• @Vaggelis_Z That's the problem... nobody wants to waste time reading the code and follow convoluted logic just because you won't take the damned 2 minutes to explain clearly in words, what it is that you want to achieve. This is exactly what about 5-6 other users were trying to tell you. I understand that your problem is important to you, but others have their own problems to worry about as well. In the midst of that, they need to also find some time to help you with your problem. The least you could do is to make things easy for them so that they needn't do the untangling themselves. – rm -rf Aug 1 '13 at 15:04
• You needn't delete this question if you don't want to — it's far more contained and understandable than the previous one, as is evidenced by the respective votes. Just incorporate whatever you've mentioned above into your MWE. The part about importing data is secondary and irrelevant to the plotting part, since you already have that working properly. You can take my suggestion and use n := RandomInteger as a blackbox "number of plots from data files" generator and refocus your question solely on how to automatically grid the n plots. – rm -rf Aug 1 '13 at 15:07

not sure if this is what you wanted to or....

Manipulate[
data = Table[ListPlot[RandomReal[{0, 1}, {20}], Joined -> True,Mesh -> True],
{nFiles}];
makeGrid[data],

{{nFiles, 3, "how many files?"}, 1, 50, 1,Appearance -> "Labeled"},(*must start at 3*)
Initialization :>
(
makeGrid[data_List] := Module[{d = data, nCol = 3, over},
over = Mod[Length[d], nCol];
If[over > 0,
d = Join[d, Flatten@Table[, {nCol - over}]]
];
GraphicsGrid[Partition[d, nCol], ImageSize -> {500, 400},Frame -> All]
];
)
] Here's a list of data of varying length num:

data[num_] := RandomReal[{-2, 2}, {num, 24, 2}];


and here's a function to grid this as closely as possible to a square:

gridgraph[p_List] := Module[{thres},
thres = Floor@Sqrt@Length@p;
GraphicsGrid@Partition[p, thres, thres, 1, Null]
] /; Length@p > 0


now

gridgraph[ListPlot /@ data] and

gridgraph[ListPlot /@ data] ---EDIT(2)---

Your list of files depends on how you import. If they are located at a path, say, "C:/DIRECTORY/" and have extensions .dat then import them:

data = With[{$filedir = "C:/DIRECTORY/"}, Import[$filedir <> #] & /@
Select[Import[\$filedir], StringMatchQ[#, "*dat"] &]]


This will be a list of eight lists (right?), some empty {} some consisting of pairs (right?).
Is the following what you need?

 Module[{plots},
plots = (ListPlot /@ data) /.
ListPlot[{}] :> Null // Quiet;
GraphicsGrid[Partition[plots, 3, 3, 1, Null]]]

• This is very close to what I want, but still we insert manually the number of plots (i.e. 5 or 21). The target is to read the lists (or external files), determine automatically the amount and then plot in a grid. – Vaggelis_Z Aug 1 '13 at 10:57
• See my edit, or if you really want take a look to the actual case in my previous post. – Vaggelis_Z Aug 1 '13 at 11:27
• so your question IS about how to import then? – gpap Aug 1 '13 at 11:31
• No, my question is how to define the size of the grid without knowing beforehand how many files are to be read and plotted. – Vaggelis_Z Aug 1 '13 at 11:35
• very close to what I want. But the data files do not have the same extension. – Vaggelis_Z Aug 1 '13 at 12:05