# Varying plot sizes for identical PlotRanges in ListLinePlot, where one has a legend

I am trying to make some figures using ListLinePlot in Mathematica 8. Although the PlotRanges are the same, the resulting frame sizes are different. The only difference (as I see it) between the two plots is that one has a Legend, while the other does not; I made sure the legend sits well within the plot and does not stick out.

I experimented with AspectRatio and ImageSize, they only modify the overall size but is not changing the frame size. I am trying to align these plots one below the other in a document, and this varying frame size is causing misalignment.

I am attaching a screenshot, and please note the "excess space" on the right hand side plot in comparison to the left.

-

## migrated from stackoverflow.comJun 24 '12 at 2:08

This question came from our site for professional and enthusiast programmers.

Just define a Legend with minimum size in the other plot:

f[x_] := x;
g[x_] := -x;
Needs["PlotLegends"];
b = Plot[{f[x], g[x]}, {x, 0, 1}, PlotLegend -> {"f[x]", "g[x]"},
LegendPosition -> {-0.2, -0.2}, LegendSize -> {0.5, 0.5},
Frame -> True];
c = Plot[{f[x], g[x]}, {x, 0, 1}, PlotLegend -> {"f[x]", "g[x]"},
LegendPosition -> {-0.2, -0.2}, LegendSize -> {0.001, 0.001},
Frame -> True];

GraphicsRow[{b, c}]


-
There's a little dot, around (0.35, -0.5). –  Chris Degnen Jun 19 '12 at 18:31
@ChrisDegnen I know. You could place it just superimposed with an axis if you want to get rid of that little imperfection. –  belisarius Jun 19 '12 at 18:38
@Belisarius interesting solution. I did not try really small numbers in LegendSize. Thanks! –  user1465835 Jun 20 '12 at 9:44

PlotLegends seems to incorporate a graphics set that increases the graphics height, in the case below from 218 to 225. Since the resulting graphics object is quite hard to resize to match the simpler plot it may be expedient to plot the first plot with a similar legend and then remove it, leaving the graphics object the same height as the one with the legend.

f[x_] := x;
g[x_] := -x;
a = Plot[{f[x], g[x]}, {x, 0, 1}];
Print[ImageDimensions[a]];
Needs["PlotLegends"];
b = Plot[{f[x], g[x]}, {x, 0, 1}, PlotLegend -> {"f[x]", "g[x]"},
LegendPosition -> {-0.2, -0.2}];
Print[ImageDimensions[b]];
c = Plot[{f[x], g[x]}, {x, 0, 1}, PlotLegend -> {"f[x]", "g[x]"},
LegendPosition -> {-0.2, -0.2}];
gr = Position[c, GraphicsGroup];
c[[Sequence @@ Most@First[gr]]] =
c[[Sequence @@ Most@First[gr]]] /. Inset[__] -> Sequence[];
c[[Sequence @@ Most@First[gr]]] =
c[[Sequence @@ Most@First[gr]]] /. GrayLevel[_] -> Opacity[0];
Print[ImageDimensions[c]];
d = Plot[{f[x], g[x]}, {x, 0, 1}, PlotLegend -> {"f[x]", "g[x]"},
LegendPosition -> {-0.2, -0.2}];
Print[ImageDimensions[d]];
GraphicsRow[{Framed@Show[c, ImageSize -> 300],
Framed@Show[d, ImageSize -> 300]}, ImageSize -> 700]


{360, 218}

{360, 225}

{360, 225}

{360, 225}

-
Thank you so much for your solution Chris! I can line my plots up correctly now! –  user1465835 Jun 20 '12 at 9:42