1
$\begingroup$

I have this plot where I assigned a specific image size

Module[{XZ = 284, YZ = 110}, 
 plot = Plot[Sin[2 x], {x, -6, 6}, Frame -> True, 
   ImageSize -> Automatic -> {XZ, YZ}, 
   LabelStyle -> {FontFamily -> "Helvetica", Black, FontSize -> 13}, 
   FrameStyle -> Black]]  

enter image description here

Then I would like to use Rasterize but need the output image to be the same size as the one from the plot

Module[{XZ = 284, YZ = 110}, 
 Rasterize[plot, RasterSize -> 800, 
  ImageSize -> Automatic -> {XZ, YZ}]]  

but the generated image is smaller? How can I make Rasterize give the same size as the original one?

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ ImageSize -> Automatic -> {XZ, YZ} is this correct? I've not seen that kind of syntax before. Are you sure you didn't mean ImageSize -> {XZ, YZ} $\endgroup$
    – flinty
    May 27, 2021 at 10:46
  • $\begingroup$ Yeah, I never liked that form. Design mistake IMO. $\endgroup$
    – ihojnicki
    May 27, 2021 at 12:55
  • 1
    $\begingroup$ @ihojnicki This form does provide an important feature which (as far as I know) is not accessible in any other way. Are there any plans to document it? $\endgroup$
    – Szabolcs
    May 27, 2021 at 13:28
  • $\begingroup$ @Szabolcs, not in this form. That much is certain. $\endgroup$
    – ihojnicki
    May 27, 2021 at 13:38

2 Answers 2

2
$\begingroup$

How can I make Rasterize give the same size as the original one?

Drop the ImageSize and RasterSize from Rasterize. Then the rasterized result will display at the same size as the original.

In order for the result to have the same pixel size, also add ImageResolution -> 72. On my "hidpi" screen, I get a double-resolution image otherwise (but it still displays at the correct size as the resolution is encoded into the image).


It is good to note that ImageSize -> Automatic -> {x,y} is an undocumented syntax that sets not the size of the entire graphics, but the size of the plot area only. This means that:

  • If you use ImageSize -> 300 then the rasterized result will be 300 pixels wide (assuming ImageResolution -> 72)
  • If you use ImageSize -> Automatic -> 300, the result will be wider than 300 pixels.
$\endgroup$
2
$\begingroup$
  1. Rasterize does not understand that form.
  2. Even if you move it back into the Plot, the coordinates are no longer accurate afterwards. You would need to post-process the result to replace ImageSize->Automatic->{284, 110} with ImageSize->Automatic.
$\endgroup$

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.