16
$\begingroup$

Where is all the vertical whitespace coming from, and how do I get rid of it?

plot = Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
  PlotLegends -> Placed[LineLegend["Expressions"], {1, 0.5}]];
GraphicsGrid[{{plot, plot}, {plot, plot}}, Spacings -> {0, 0}]

enter image description here

EDIT Okay, AspectRatio is definitely the key for the GraphicsGrid option (which I'm pursuing in case I don't want to rasterize). It seems I get rid of all whitespace if:

  1. Use AspectRatio->Full in both the plot and the GraphicsGrid
  2. Use numerical placement of the legends...not tags like "Before", etc.

What I've come up with so far would get with hairy frame labels, or tick/legend text that varied in length, but I think those things are beyond the scope of this post:

leftPlot = 
  Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, FrameTicks -> All,
    AspectRatio -> Full, Background -> Green, 
   PlotRange -> {{-0.5, 10.5}, {-1.1, 1.1}}, 
   PlotLegends -> 
    Placed[LineLegend["Expressions"], {{0, 0.5}, {1, 0.5}}]];
rightPlot = 
  Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, FrameTicks -> All,
    AspectRatio -> Full, Background -> Green, 
   PlotRange -> {{-0.5, 10.5}, {-1.1, 1.1}}, 
   PlotLegends -> 
    Placed[LineLegend["Expressions"], {{1, 0.5}, {0, 0.5}}]];
GraphicsGrid[
 {{
   Show[leftPlot, 
    ImagePadding -> {{Automatic, None}, {None, Automatic}}],
   Show[rightPlot, 
    ImagePadding -> {{None, Automatic}, {None, Automatic}}]
   },
  {
   Show[leftPlot, 
    ImagePadding -> {{Automatic, None}, {Automatic, None}}],
   Show[rightPlot, 
    ImagePadding -> {{None, Automatic}, {Automatic, None}}]
   }},
 Spacings -> {0, 0}, Frame -> True, AspectRatio -> Full, 
 ImageSize -> 72*{8, 4}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Adjust AspectRatio parameter inside your Plot function. For example AspectRatio -> 1. Maybe move your legend down. $\endgroup$
    – swish
    Oct 17, 2013 at 20:10
  • 1
    $\begingroup$ You could try Grid instead of GraphicsGrid. $\endgroup$
    – wxffles
    Oct 17, 2013 at 20:30

3 Answers 3

14
$\begingroup$

You might consider using Grid in place of GraphicsGrid. Doing so comes with both pluses and minuses, so it is not a no-brain choice. However, Grid will accommodate any shape and size of display object automatically. When putting plots in a grid, I find it is usually best to set ImageSize in the plot explicitly.

Using your example plot at Medium size

plot = Plot[{Sin[x], Cos[x]}, {x, 0, 10},
  Frame -> True, ImageSize -> Medium,
  PlotLegends -> Placed[LineLegend["Expressions"], {1, 0.5}]]
Grid[{{plot, plot}, {plot, plot}},
 Frame -> All, Spacings -> {0, 0}]

give this

plot.png

You can see there is no wasted space.

$\endgroup$
2
  • 1
    $\begingroup$ That does look a lot better. I take it one of the minuses you allude to is that Grid cannot be exported as an image the way GraphicsGrid can? Good trick to know in any case. $\endgroup$
    – brad
    Oct 18, 2013 at 16:06
  • 2
    $\begingroup$ @brad You can export it just the same, after running the code above Export["grid.png", %] pretty much everything can be exported as an image thanks to Rasterize $\endgroup$
    – ssch
    Oct 20, 2013 at 15:47
12
$\begingroup$

The main problem is that the aspect ratio of the plot and its container are different. You can still use GraphicsGrid with the correct settings:

plot = Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
   PlotLegends -> Placed[LineLegend["Expressions"], {.5, 0.5}], 
   Background -> Green, AspectRatio -> 1/GoldenRatio];
GraphicsGrid[{{plot, plot}, {plot, plot}}, Spacings -> {0, 0}, 
 Frame -> All, AspectRatio -> 1/GoldenRatio]

enter image description here

By the way, have you seen my answer to your other question?

$\endgroup$
3
  • 1
    $\begingroup$ I'm sorry, but why would one want to place the legend in the middle of a plot...? $\endgroup$
    – Leo Fang
    Oct 20, 2013 at 16:22
  • 1
    $\begingroup$ The placement of the legend in this particular post is bad, and so is background color; both were done so on purpose.The awful background helps to identify those pixels wasted by other reasons. Try Spacings with negative numbers to remove them. The placement of the legends was obnoxiously placed in the middle to indicate that extra space can be saved by placing the legend inside the graph. You might want to find a better place for it. $\endgroup$
    – Hector
    Oct 20, 2013 at 16:47
  • $\begingroup$ @Hector Yes, I'm looking over that now. You can probably guess that my overall concern is how to keep the convenience of PlotLegends in situations where I want arrays of plots...To LevelScheme or not to LevelScheme, that is the question. $\endgroup$
    – brad
    Oct 21, 2013 at 1:15
2
$\begingroup$

Since there still is no accepted answer, although some good ones were given, I try and give my contribution.

While trying to solve a problem similar to yours, I came across this page where it is explained how to realise a grid of plots as follows:

plot = Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
ImageSize -> Medium, 
PlotLegends -> LineLegend["Expressions"]]

ResourceFunction["PlotGrid"][
 { {plot,plot} , {plot,plot} }, 
 Spacings -> 25,
 PlotLabels -> {
   Placed[{"top left"}, {Left, Above}],
   Placed[{"top right"}, {Center, Above}],
   "Label 3",
   "Last Label"
   }
 ]

This results in the following plot: Example plot

As you can see, the legends of each plot are collected, but that is an easy one to fix:

ResourceFunction["PlotGrid"][
 {
  {
   Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
    ImageSize -> Medium, 
    PlotLegends -> Placed[ "Expressions", {1, .5}]],
   Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
    ImageSize -> Medium]
   },
  {
   Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
    ImageSize -> Medium],
   Plot[{Sin[x], Cos[x]}, {x, 0, 10}, Frame -> True, 
    ImageSize -> Medium]
   }
  },
 Spacings -> 30,
 PlotLabels -> {
   Placed[{"top left"}, {Left, Above}],
   Placed[{"top right"}, {Center, Above}],
   "Label 3",
   "Last Label"
   }
 ]

Resulting in:

enter image description here

$\endgroup$
3
  • $\begingroup$ Can you please add an image of the result? $\endgroup$
    – Syed
    Mar 8, 2022 at 16:20
  • $\begingroup$ @Syed I have edited my answer. Sorry, I firstly posted the answer in a rush :) $\endgroup$
    – Enrico
    Mar 10, 2022 at 9:59
  • 1
    $\begingroup$ It is more beneficial for visitors to the page this way. Many thanks. $\endgroup$
    – Syed
    Mar 10, 2022 at 10:01

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.