2
$\begingroup$

Here's a contrived example of what I'm trying to do. Basically, I want a function to return some object that has two plots next to each other (or one on top of the other), both large, and both with legends.

test1 = Table[Table[{x, a*x^2}, {x, -10, 10}], {a, 2}]
test2 = Table[Table[{x, b*x^4}, {x, -10, 10}], {b, 3}]
testplot1 = ListPlot[test1];
testplot2 = ListPlot[test2];
GraphicsGrid[{{testplot1, testplot2}}]

That's good, I like how large they are on my screen, and it's cool that they're next to each other.

But if I add PlotLegends:

test1 = Table[Table[{x, a*x^2}, {x, -10, 10}], {a, 2}]
test2 = Table[Table[{x, b*x^4}, {x, -10, 10}], {b, 3}]
testplot1 = ListPlot[test1, PlotLegends -> Automatic];
testplot2 = ListPlot[test2, PlotLegends -> Automatic];
GraphicsGrid[{{testplot1, testplot2}}]

I get this fun stuff:

enter image description here

This is really annoying. Adding ImageSize->Large to GraphicsGrid doesn't help. How can I make them larger but still have legends? Thank you.

$\endgroup$
  • $\begingroup$ I'm assuming you have to assign a GraphicsGrid size. If you assign a ListPlot size instead you avoid this issue entirely(I'm not quiet sure if it is technicaly bug). $\endgroup$ – William Sep 15 '15 at 21:43
  • $\begingroup$ @William, thanks for the comment. Sorry, I'm not totally sure what you mean: how do you assign listplot a size? I didn't know that you could use imagesize as an option for it. $\endgroup$ – YungHummmma Sep 15 '15 at 21:45
  • $\begingroup$ Please let me know if Rasterizing them is an issue and I'll delete my answer. $\endgroup$ – William Sep 15 '15 at 21:54
  • $\begingroup$ How about testplot1 = ListPlot[test1, ImageSize -> 200, PlotLegends -> Automatic]; testplot2 = ListPlot[test2, ImageSize -> 200, PlotLegends -> Automatic]; GraphicsGrid[{{testplot1, testplot2}}, ImageSize -> 500]. $\endgroup$ – march Sep 15 '15 at 22:17
  • $\begingroup$ Which version are you using? I don't see any issues for 10.2 on Win 8.1 Pro 64-bit. $\endgroup$ – Edmund Sep 16 '15 at 10:09
1
$\begingroup$

March's comment works well.

testplot1 = ListPlot[test1, PlotLegends -> Automatic, ImageSize -> 200];
testplot2 = ListPlot[test2, PlotLegends -> Automatic, ImageSize -> 200];
GraphicsGrid[{{testplot1, testplot2}}, ImageSize -> 500,
       Background -> RGBColor[0, 1, 0, 0.1]]

Mathematica graphics

Note that it has some space above and below the plot.

Sometimes when I want finer control over the graphics I use Row, Column or Grid rather than the graphics counterpart.

Row[{testplot1, Spacer[20], testplot2},
      Background -> RGBColor[0, 1, 0, 0.1]]

Mathematica graphics

You can add space to the left, middle or right using Spacer and control the vertical space by making the spacer parameters a list. For example

Spacer[{20,250,100}]

will create a space 20 points wide, 250 points high and 100 points below the baseline.

In the example below we add a margin of 10 points on each side, 20 points between the plot and 50 points above (i.e., 250-200) and 100 points below.

Row[{
  Style[Spacer[{10, 20}], Background -> Red],
  testplot1,
  Style[Spacer[{20, 250, 100}], Background -> Red],
  testplot2,
  Style[Spacer[{10, 20}], Background -> Red]
  },
  Background -> RGBColor[0, 1, 0, 0.1]
 ]

Mathematica graphics

The color is just to show the Spacer. Color would not be used in a production plot.

$\endgroup$
0
$\begingroup$

If you rasterize them its easy as Pi.

test1 = Table[Table[{x, a*x^2}, {x, -10, 10}], {a, 2}]
test2 = Table[Table[{x, b*x^4}, {x, -10, 10}], {b, 3}]
testplot1 = 
  Rasterize[ListPlot[test1, PlotLegends -> Automatic], 
   ImageSize -> 600];
testplot2 = 
  ListPlot[test2, PlotLegends -> Automatic, ImageSize -> 600];
GraphicsGrid[{{testplot1, testplot2}}]
$\endgroup$
  • $\begingroup$ I don't think we want to Rasterize any of the plots or components, lest we get aliasing when rescaling them for publication or display at a different size. Isn't there another way to avoid the scaling problem identified by the question poser? $\endgroup$ – David G. Stork Sep 15 '15 at 22:06
  • $\begingroup$ @DavidG.Stork If you find one please post it. I recall issues with this but don't remember the solution $\endgroup$ – William Sep 15 '15 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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