4
$\begingroup$

I am trying to export a GraphicsGrid to a PDF such that the PDF has a size of 8x10 inches and a resolution of 300 dpi. Here is what I have done:

  1. Create the graphics grid with ImageSize->Full to get all the images placed in the grid correctly.

  2. Rasterize it to RasterSize 300{8,10}.

grid = Rasterize[
  GraphicsGrid[{
    {fiberAndReferencePlot}, 
    {transmissionPlot},
    {summaryPlotRadians}
   }, 
   ImageSize -> Full
   ], 
  RasterSize -> 300 {8, 10}
  ]
  1. Export it to the correct size 8x10.
Export["Grid.pdf", grid, ImageSize -> 72 {8, 10}]

When I look at the PDF with Photoshop, it tells me that the image size in pixels is 2400x3000, which is correct. But the size in inches is 6.3 x 7.875, which is not the 8 x 10 I expected.

Can anyone tell me how to do this properly?

EDIT:

Here is something else I tried. I create a rasterized image of the desired pixel dimensions. Then I export telling export the ImageResolution is 300. I hoped that export would then respect the size of the raster, and calculate an image size in inches based on the specified image resolution.

im1 = Plot[Sin[x], {x, 0, 4 Pi}, Frame -> True, PlotLabel -> "Sin(x)",
    FrameLabel -> {"Angle", "Value"}];

g = GraphicsGrid[{{im1}, {im1}, {im1}}];

gr = Rasterize[g, RasterSize -> 1500];

Dimensions@ImageData@gr

(* {2696,1500,3} *)

pdf = Export["gr.pdf", gr, ImageResolution -> 300];

When I look at the exported PDF with Photoshop, it tells me the PDF has a size of 1080 x 1941 pixels in 3.6 x 6.47 inches. This does not seem to have any relationship to what I wanted.

What I am trying to do should be very simple. I want to take a graphic displayed on the screen and export it at a specific resolution and image size.

$\endgroup$
6
  • $\begingroup$ I tried to put code in code blocks using ctrl-k, but this is what I got?! $\endgroup$ Nov 23, 2020 at 19:06
  • $\begingroup$ Are you sure your display is 72 pixels / inch? Like there's a whole world of annoying conversions between printers points & pixels and in 12.1 I think Mathematica subtly changed the way it does this $\endgroup$
    – b3m2a1
    Nov 23, 2020 at 19:14
  • $\begingroup$ Thanks b3. I’m sure it isn’t. More like 140 dpi. But I’ve tried using that with no good results. I edited my post with another attempt. How does mma determine my display resolution? $\endgroup$ Nov 23, 2020 at 22:19
  • 1
    $\begingroup$ @DavidKeith Export as PDF is assuming the print environment, which applies a number of different styles/options. The only way you can get what you are looking for is by using Save Selection As > PDF. Please report this to support and I'll look into more. $\endgroup$
    – ihojnicki
    Nov 24, 2020 at 0:59
  • 1
    $\begingroup$ Thank you. I can’t use save selection because I want to use this for saving a summary sheet for each data set which are being batch processed. I have sent a simplified query to WRI and will report what I get from them. $\endgroup$ Nov 24, 2020 at 1:25

1 Answer 1

2
$\begingroup$

Well exporting graphics from Mathematica is always a painful process. The program is not designed well for outputting graphics/generating complex plots easily at least when compared to something like Pythons matplotlib package. After this short rant a view practical suggestions.

  1. Set the printing environment to Working (either using the front end: File->Print Settings->Printing Environment->Working or using SetOptions[$FrontEndSession,PrintingStyleEnvironment -> "Working"]). This way the plots inside the notebook at least somewhat resemble the ones exported and one can start tweaking.
  2. Use cmToPt[cm_]:=QuantityMagnitude@UnitConvert[Quantity[cm,"cm"],"PrinterPoints"] or inchesToPt[inches_]:=QuantityMagnitude@UnitConvert[Quantity[inches,"Inches"],"PrinterPoints"] to convert from to physical units.
  3. Use ImageSize and ImageResolution for rasterized PDF-export.

After those rather general remarks here a possible solution to OPs specific question:

im1 = Plot[Sin[x], {x, 0, 4 Pi}, Frame -> True, PlotLabel -> "Sin(x)",FrameLabel -> {"Angle", "Value"}];
g = GraphicsGrid[{{im1}, {im1}, {im1}}];
gr = Rasterize[g, ImageResolution -> 300, ImageSize -> inchesToPt[8]]
pdf = Export["gr_300.pdf", gr]

ImageData[gr] // Dimensions returns {4313, 2400, 3} which is consistent with the specified width of 8 inches at a resolution of 300pt per inch. The exported PDF has a size of 8 times 14.4 inches and when I import the PDF into a graphics program (making sure to import the image only) I get the 2400 times 4313 Pixel image size.

For the GraphicsGrid in question this solution works surprisingly well but in my experience working with more complicated (composite) graphics (including Legends, more exotic plots, ...) this usually is not the case when exporting graphics (especially vector graphics) from Mathematica. One shortcoming of the current answer is that the aspect ratio of the image is basically given by g. It might be possible to get RasterSize to work but the documentation states that RasterSize overwrites ImageResolution which to me seems necessary when exporting rasterized content to PDF.

$\endgroup$
3
  • 1
    $\begingroup$ When I execute the same code I also get 2400 x 4313 for the dimensions of ImageData [gr]. But after I export the PDF is 5.76 wide and 10.35 tall with pixels 1728 x 3105. Maybe there is some issue with printer points on my machine? $\endgroup$ Nov 24, 2020 at 20:14
  • $\begingroup$ What version of Mathematica do you use and have you set SetOptions[$FrontEndSession,PrintingStyleEnvironment -> "Working"] before evaluating the code? $\endgroup$
    – N0va
    Nov 24, 2020 at 20:18
  • $\begingroup$ Yes. I tried to replicate your process exactly. I'm on 12.1.1 for Win10 x64. I have seen some references to setting display resolution for the new implementation, but I have not figured out how that is done. Maybe it makes a difference. $\endgroup$ Nov 24, 2020 at 22:11

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.