3
$\begingroup$

I have this figure produced using Plot and Grid

 fig = Plot[Sin[z], {z, -2 Pi, 2 Pi}, Frame -> True, 
  FrameLabel -> {"x", "y"}, ImageSize -> Automatic -> {300, 200}, 
  LabelStyle -> {Black, FontSize -> 14}, PlotStyle -> Red, 
  ImagePadding -> 80];  

dfr = Grid[{{fig, fig}, {fig, fig}}, Spacings -> {-12, -12}, 
  Frame -> False]   

enter image description here

Now this figure can be exported as pdf but there are extra white spaces coming from ImagePadding. How can I remove the white space efficiently from the final figure?

$\endgroup$
2
  • $\begingroup$ This? dfr = Grid[{{fig, fig}, {fig, fig}} /. (ImagePadding -> 80) -> ImagePadding -> Automatic, Frame -> False] $\endgroup$ Jul 12, 2020 at 19:34
  • $\begingroup$ @RohitNamjoshi Well, the point is that I want to keep the four panels aligned as in the figure above, what you suggested destroys the alignment. $\endgroup$
    – MMA13
    Jul 12, 2020 at 19:44

2 Answers 2

4
$\begingroup$

If the frame and tick labels are the same for each grid element, you can pre-process the input to specify different option values for FrameLabel, FrameTicks and ImagePadding for border and internal elements:

ClearAll[processGrid]
processGrid[padding_: {{Scaled[.05], Scaled[.0025]}, {Scaled[.05], Scaled[.0025]}}, 
  flabel_: {"x", "y"}, fticks_: {{Automatic, Automatic}, {Automatic, Automatic}}] = 
  Module[{dims = Dimensions @ #, p = padding[[1, 2]], 
       sft = Charting`ScaledFrameTicks[{Identity, Identity}]}, 
    MapIndexed[Switch[#2, 
       {dims[[1]], 1},  Show[#, FrameTicks -> fticks, ImagePadding -> padding, 
        FrameLabel -> flabel], 
       {_, 1}, Show[#, FrameTicks -> ReplacePart[fticks, {2, 1} -> sft], 
        FrameLabel -> ReplacePart[flabel, {1} -> None], 
        ImagePadding -> ReplacePart[padding, {2, 1} -> p]],
       {dims[[1]], _}, Show[#, FrameTicks -> ReplacePart[fticks, {1, 1} -> sft], 
        FrameLabel -> ReplacePart[flabel, {2} -> None], 
        ImagePadding -> ReplacePart[padding, {1, 1} -> p]], 
       {_, _}, Show[#, FrameTicks -> sft, FrameLabel -> None, 
        ImagePadding -> p]] &, #, {2}]] &;

Examples:

fig1 = Plot[Sin[z], {z, -2 Pi, 2 Pi}, Frame -> True, 
   FrameLabel -> {"x", "y"}, ImageSize -> Automatic -> {300, 200}, 
   LabelStyle -> {Black, FontSize -> 14}, PlotStyle -> Red];

grid = ArrayReshape[{fig1}, {2, 2}, "Fixed"];

Grid[processGrid[][grid], Spacings -> {0, 0}]

enter image description here

Grid[processGrid[][ArrayReshape[{fig1}, {3, 2}, "Fixed"]], Spacings -> {0, 0}]

enter image description here

You can specify padding in printer points:

Grid[processGrid[{{60, 10}, {60, 10}}][
  ArrayReshape[{fig1}, {2, 2}, "Fixed"]], Spacings -> {0, 0}]

enter image description here

$\endgroup$
0
2
$\begingroup$

You can also use ResourceFunction["PlotGrid"] (requires Mathematica 11.3 or higher), which was designed for exactly this purpose:

fig = Plot[Sin[z], {z, -2 Pi, 2 Pi}, Frame -> True, 
  FrameLabel -> {"x", "y"}, ImageSize -> Automatic -> {300, 200}, 
  LabelStyle -> {Black, FontSize -> 14}, PlotStyle -> Red]

ResourceFunction["PlotGrid"][
 {{fig, fig}, {fig, fig}},
 Spacings -> 20,
 "ShowFrameLabels" -> Automatic
 ]

enter image description here

Note that I have removed the ImagePadding->80 setting from fig, as it is not necessary (if you want to keep it, you could also set a custom ImagePadding as PlotGrid option instead). Also note how the labels are automatically hidden where necessary (this is controlled by the "ShowFrameLabels" option).

Alternatively, you can use GraphicsGrid in version 12.0 or higher to get similar results, although with a bit more manual work required.

$\endgroup$
2
  • $\begingroup$ I got this error: Function::slota: Named Slot x in Association[ImagePadding->Abs[#is-#pr],ImageSize->Abs[Apply[Subtract,#is,{1}]],PlotRangeSize->Abs[Apply[Subtract,#pr,{1}]],PlotRange->{#x,#y}]& cannot be filled from <|pr->{{74.5,429.},{229.593,10.5}},is->{{74.,434.},{231.093,10.}}|>. On M.12.0.0? $\endgroup$
    – MMA13
    Jul 13, 2020 at 11:42
  • 1
    $\begingroup$ @HD2006 Are you using a non-standard Evaluator for the notebook (in the Evaluation > Notebook's kernel)? The current version is unfortunately broken if you use anything other than the default (which should be "Local", you can create a new notebook and see what the default is for you by checking which evaluator is set there) $\endgroup$
    – Lukas Lang
    Jul 13, 2020 at 12:58

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.