7
$\begingroup$

I'm running some code based on @R.M.'s answer to this question to create a couple plots side by side.

I want to export the plots rasterized to a png. However, when I Rasterize the plot at a higher RasterSize or ImageResolution it splits the row on to two seperate lines. See the following simplified example below:

Rasterize[
 With[{size = 250}, 
  Row[Show[#, ImageSize -> {Automatic, size}, 
      ImagePadding -> {{30, 15}, {40, 5}}] & /@ {Plot[
      Sin[x], {x, -1, 1}], Plot[Sin[x], {x, -1, 1}]}]], 
 ImageResolution -> 200]

It looks like this:

Mathematica graphic

I want both plots on the same line but at higher resolution, like this:

Mathematica graphic

What is going wrong?

$\endgroup$
4
$\begingroup$

It is possible to give an explicit ImageSize to Row and if it is large enough to contain the graphics it will not wrap. If it is given in the form {maximum} it will be sized automatically. Infinity does not appear to work so I used 1*^6:

Rasterize[
 With[{size = 250}, 
  Row[Show[#, ImageSize -> {Automatic, size}, 
      ImagePadding -> {{30, 15}, {40, 5}}] & /@ {Plot[
      Sin[x], {x, -1, 1}], Plot[Sin[x], {x, -1, 1}]}, 
    ImageSize -> {1*^6}]],
 ImageResolution -> 200]
$\endgroup$
  • $\begingroup$ Although it seems a bit "hackish" this was the easiest and worked perfectly. Thanks Mr. Wizard! $\endgroup$ – s0rce Jul 31 '12 at 16:42
  • $\begingroup$ @s0rce glad I could help. :-) $\endgroup$ – Mr.Wizard Jul 31 '12 at 16:54
  • 1
    $\begingroup$ @Mr.Wizard Thank you. Mathematica will create wrapped EPS files when not rasterized, even if right-clicking the Row-created graphics cell bracket and using "Save as...". Your solution works even without the Rasterize command. $\endgroup$ – Ghersic Oct 24 '18 at 19:08
5
$\begingroup$

Use GraphicsRow to avoid breaking the row. You can control the resolution of your exported image with the option ImageSize -> ... supplied directly to GraphicsRow. This is another advantage of GraphicsRow over Raw - it takes ImageSize option. Yet with most of other objects using plane Row is more preferable.

And though I did not use Rasterize, and exported directly, you can still use it if you want to.

Export["test.png", With[{size = 250}, 
GraphicsRow[Show[#, ImageSize->{Automatic, size},ImagePadding->{{30, 15}, {40, 5}}] & /@ 
  {Plot[Sin[x], {x, -1, 1}], Plot[Sin[x], {x, -1, 1}]},Spacings-> 0]], ImageSize -> 500]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ GraphicsRow has it's own problems: the two cells will be the same width even if the graphics are not. This is what motivated my question that s0rce is linking to. $\endgroup$ – Szabolcs Jul 26 '12 at 15:22
  • $\begingroup$ @Szabolcs Yes, true, but this questions has identical plots. $\endgroup$ – Vitaliy Kaurov Jul 26 '12 at 17:45
5
$\begingroup$

You can change from Row over to Grid, which does no linebreaking. Then you just need to add another set of curly braces around your former input row:

Rasterize[
 With[{size = 250}, 
  Grid[{Show[#, ImageSize -> {Automatic, size}, 
       ImagePadding -> {{30, 15}, {40, 5}}] & /@ {Plot[
       Sin[x], {x, -1, 1}], Plot[Sin[x], {x, -1, 1}]}}]], 
 ImageResolution -> 200]

Mathematica graphics

$\endgroup$

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.