2
$\begingroup$

If I try

Graphics[Line[{{0, 0}, {0, 1}}], ImageSize -> 150, AspectRatio -> 1]

then I get

enter image description here

This has a lot unnecessary white space on both sides of the line.

If I try

Graphics[Line[{{0, 0}, {0, 1}}], ImageSize -> {50,150}, AspectRatio -> 1]

then I get

enter image description here

The size of the image is Okay. But then the line becomes very small. What is the relationship between the line's coordinates and ImageSize? Why the are the two picutres so different?

If I try

Graphics[Line[{{0, 0}, {0, 1}}], ImageSize -> {50,150}, AspectRatio -> Full]

Then it seems to be alright.

enter image description here

But once I add some points, there is a problem. See the two results

Graphics[{{PointSize[0.3], Point[{0, 0}]}, Line[{{0, 0}, {0, 1}}]}, ImageSize -> {50, 150}, AspectRatio -> Full, ImagePadding -> 10, Frame -> True]

enter image description here

Graphics[{{PointSize[0.3], Point[{0, 0}]},   Line[{{0, 0}, {0, 1}, {1, 1}}]}, ImageSize -> {150, 150},  AspectRatio -> Full, ImagePadding -> 10, Frame -> True]

enter image description here

You see the two points are of different sizes. What is going on here?

$\endgroup$
1
$\begingroup$

What you are seeing are the kind of problems that you run into if you attempt to set both ImageSize and AspectRatio.

Graphics[
 Line[{{0, 0}, {0, 1}}],
 ImageSize -> {50, 150},
 AspectRatio -> 1,
 Frame -> True
 ]

Screenshot

ImageSize is the full size of the graphics, e.g. the orange box in the image here above. AspectRatio is the ratio of the box that frames the graphics. You're specifying that the box should have the aspect ratio 1, i.e. be a square, but the image should have the aspect ratio 150/50=3. Clearly, there's going to be a lot of white space on the top and bottom of the graphics to make that happen.

We can make the figure fill the image by making the aspect ratio of the figure match the aspect ratio of the image:

Graphics[
 Line[{{0, 0}, {0, 1}}],
 ImageSize -> {50, 150},
 AspectRatio -> 150/50,
 ImagePadding -> 0,
 Frame -> True
 ]

Mathematica graphics

Now the framed box takes up the entire area of the image. Unfortunately, it leaves the axis tick labels outside the plot. To fix this, ImagePadding is used:

Graphics[
 Line[{{0, 0}, {0, 1}}],
 ImageSize -> {50, 150},
 AspectRatio -> 150/50,
 ImagePadding -> {{20, 5}, {20, 5}},
 Frame -> True
 ]

Screenshot 2

This fixed the labels, but now we see that we have white space around the image again. This is because when we add padding, we decrease the size of the figure by the same amount. This is as opposed to adding margin, which keeps the size of the figure the same but increases the image size. I've made a tool to explore these effects, available here.

We need to adapt the aspect ratio according to our plot padding:

Graphics[
 Line[{{0, 0}, {0, 1}}],
 ImageSize -> {50, 150},
 AspectRatio -> (150 - 25)/(50 - 25),
 ImagePadding -> {{20, 5}, {20, 5}},
 Frame -> True
 ]

Screenshot

Now the figure fills up the entire image again. I hope this gives you an understanding of the issues that you are facing.

As for the size of the points in your images, you should note that the sizes of graphics primitives are given in the coordinate system of the figure. If you look at the axes of the frame, you will see that in this coordinate system the two points are the same size. One appears larger than the other because of how the coordinate system has been stretched in one of the images compared to the other.

$\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.