3
$\begingroup$

I am having problems joining multiple plots using GraphicsRow and GraphicsGrid. The issue is that Mathematica (v12.1) rescales arbitrarily my plots when I try to combine them. The issue arises when I try to combine the output of GraphicsGrid with another plot using again GraphicsGrid/GraphicsRow, even if (I believe) I properly take into accout image sizes and paddings.

My goal is the following: After I put six identical plots in grid, I would like to add a seventh plot to the right, which has different dimensions and then export everything to .pdf.

I understand that if I want to preserve the dimensions of the plots with GraphicsGrid/GraphicsRow, I have to correctly specify the aspect ratio and ImageSize of all the plots. I define the image sizes of my plots to be the sum of the frame size and image padding, like in the following code:

data = Table[RandomReal[], {10}]; (* Some data to plot *)

(* Defining relevant dimensions for plot 1*)
framex1 = 100;
framey1 = 100;

paddingx1 = {50, 10};
paddingy1 = {50, 10};

imagepadding1 = {paddingx1, paddingy1}
imagesize1 = {framex1 + Total[paddingx1], framey1 + Total[paddingy1]}


plot1 = ListPlot[data, PlotMarkers -> Automatic,
  Frame -> True, FrameLabel -> {"foo", "bar"},
  AspectRatio -> framey1/framex1, ImageSize -> imagesize1, 
  ImagePadding -> imagepadding1]

This code produces this plot:

enter image description here

Then I put six copies of this plot in a grid:

gridplot = 
 GraphicsGrid[{{plot1, plot1, plot1}, {plot1, plot1, plot1}}, 
  Spacings -> 0, ImageSize -> {3 imagesize1[[1]], 2 imagesize1[[2]]}]

This works fine. In this case gridplot actually shows 6 identical copies of plot1, with the correct size. The result is:

enter image description here

Now I consider the additional plot. I am careful to set the vertical size to be equal to the one of gridplot, setting the vertical padding accordingly:

(* Defining relevant dimensions for plot 2*)
framex2 = 100;
framey2 = 150;

paddingx2 = {50, 10};
paddingy2 = {(2 imagesize1[[2]] - framey2)/
    2, (2 imagesize1[[2]] - framey2)/2};

imagepadding2 = {paddingx2, paddingy2}
imagesize2 = {framex2 + Total[paddingx2], 2 imagesize1[[2]]}


plot2 = ListPlot[data, PlotMarkers -> Automatic,
  Frame -> True, FrameLabel -> {"foo", "bar"},
  AspectRatio -> framey2/framex2, ImageSize -> imagesize2, 
  ImagePadding -> imagepadding2]

Now, if I put gridplot and plot2 side-by-side, I see that the dimensions are correct. In particular the list

{gridplot, plot2}

gives:

enter image description here

However, If I now use GraphicsRow the plots are different and the image padding is messed up!

plot = GraphicsRow[{gridplot, plot2}, Spacings -> 0, 
  ImageSize -> {3 imagesize1[[1]] + imagesize2[[1]], 
    2 imagesize1[[2]]}]

enter image description here

What am I getting wrong? I have the feeling it might have to do with ImagePadding passed to GraphicsRow/GraphicsGrid.

One solution would be to rasterize the images before using GraphicsRow/GraphicsGrid, but this would prevent to export the final result in vectorial form.

Any help would be appreciated!

$\endgroup$

1 Answer 1

2
$\begingroup$

Here is one solution

plot1 = With[{XZ = 100, YZ = 100}, 
   ListPlot[data, Frame -> True, FrameLabel -> {"foo", "bar"}, 
    PlotMarkers -> Automatic, ImageSize -> Automatic -> {XZ, YZ}, 
    ImagePadding -> 80]];
plot2 = With[{XZ = 100, YZ = 203}, 
   ListPlot[data, Frame -> True, PlotMarkers -> Automatic, 
    FrameLabel -> {"foo", "bar"}, ImageSize -> Automatic -> {XZ, YZ}, 
    ImagePadding -> 80]];
f = Grid[{{plot1, plot1, plot1}, {plot1, plot1, plot1}}, 
   Spacings -> {-12, -12}];
Grid[{{f, plot2}}, Spacings -> {-7, -12.}, BaselinePosition -> Bottom]  

enter image description here

you can also do the other way

plot1 = With[{XZ = 100, YZ = 100}, 
   ListPlot[data, Frame -> True, FrameLabel -> {"foo", "bar"}, 
    PlotMarkers -> Automatic, ImageSize -> Automatic -> {XZ, YZ}, 
    ImagePadding -> 80]];
plot2 = With[{XZ = 310, YZ = 100}, 
   ListPlot[data, Frame -> True, PlotMarkers -> Automatic, 
    FrameLabel -> {"foo", "bar"}, ImageSize -> Automatic -> {XZ, YZ}, 
    ImagePadding -> 80]];
f = Grid[{{plot1, plot1, plot1}, {plot1, plot1, plot1}}, 
   Spacings -> {-12, -12}];
Grid[{{f}, {plot2}}, Spacings -> {-12, -7.}, 
 BaselinePosition -> Bottom]

enter image description here

$\endgroup$
2
  • $\begingroup$ Yes, this actually solves the problem because we are using Grid instead of GraphicsGrid. Indeed, if you try to export the output of Grid/Row/Column to pdf it produces the correct output with the correct proportions, even with my example. I don't know why GraphicsGrid still doesn't work, though. Thanks! $\endgroup$
    – Francesco
    Mar 31, 2020 at 20:31
  • $\begingroup$ I see, I don not know why is that happening! $\endgroup$ Apr 5, 2020 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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