Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
Adjust AspectRatio parameter inside your Plot function. For example AspectRatio -> 1. Maybe move your legend down. –  swish Oct 17 '13 at 20:10
1  
You could try Grid instead of GraphicsGrid. –  wxffles Oct 17 '13 at 20:30

2 Answers 2

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.

share|improve this answer
1  
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. –  brad Oct 18 '13 at 16:06
2  
@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 –  ssch Oct 20 '13 at 15:47

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?

share|improve this answer
1  
I'm sorry, but why would one want to place the legend in the middle of a plot...? –  Leo Fang Oct 20 '13 at 16:22
1  
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. –  Hector Oct 20 '13 at 16:47
    
@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. –  brad Oct 21 '13 at 1:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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