# How to scale and shift the coordinates of a Graphics object?

I am trying to overlap two Graphics objects g1 and g2 with Show. However, I found that when the coordinates of each object is defined to quite different ranges, I need to "scale" and "shift" the coordinates of one object to get the desired look.

For example,

g1 = Graphics[{GrayLevel[0.8], Rectangle[{-2, -2}, {2, 2}]}];
g2 = Graphics[{{GrayLevel[0.5], Rectangle[{0, 0}, {1, 1}]}}];
Show[g1, AspectRatio -> 1, Axes -> True, ImageSize -> 230]
Show[g2, AspectRatio -> 1, Axes -> True, ImageSize -> 230]
Show[g1, g2, AspectRatio -> 1, Axes -> True, ImageSize -> 230]


The overlapped version of g1 and g2 looks like this However, I would like to scale the coordinates of g2 to make g2 twice large and also shift its coordinates so the center can "roughly" coincide the center of g1. I say "roughly" because g1 and g2 may be some graphics not of a regular shape. The desired result will look like So how can I manipulate the coordinates of g2 to adjust its relative position and size when Show with g1? Please avoid modifying the definition of g1 and g2 as they can be any Graphics copy-pasted over.

You can use Scale on First @ g2 (which contains the graphics directives and primitives):

Show[g1, Graphics @ Scale[First @ g2, 2, {1, 1}], AspectRatio -> 1,
Axes -> True, ImageSize -> 230] Alterantively, you can use combination of Scale and Translate:

Show[g1, Graphics @ Translate[Scale[First @ g2, 2], -{1, 1}/2],
AspectRatio -> 1, Axes -> True, ImageSize -> 230]


or

Show[g1, Graphics@Translate[Scale[First@g2, 2, {0, 0}], -{1, 1}],
AspectRatio -> 1, Axes -> True, ImageSize -> 230]


same picture

• Thanks @kglr for the answer. Could you explain how the second argument of Translate is determined? Why it is -{1, 1}/2 instead of -{1, 1}? May 16 '19 at 20:07
• @nanjun, if you use Scale[First@g2, 2, {0, 0}] (with the third argument specifying the point that is kept fixed) you can use -{1,1} instead of -{1,1}/2) , that is, you can use Show[g1, Graphics@Translate[Scale[First@g2, 2, {0, 0}], -{1, 1}], AspectRatio -> 1, Axes -> True, ImageSize -> 230]
– kglr
May 16 '19 at 20:33
• ... since Scale[First@g2, 2] fixes the center ({1/2,1/2}) the origin of the rectangle is already shifted by -{1/2,1/2} before Translate is applied. (Compare Show[g1, Graphics@Scale[First@g2, 2], AspectRatio -> 1, Axes -> True, ImageSize -> 230] and Show[g1, Graphics@Scale[First@g2, 2, {0,0}], AspectRatio -> 1, Axes -> True, ImageSize -> 230]
– kglr
May 16 '19 at 20:37
• Thank you @kglr for the explanation, which was very clear. May 16 '19 at 20:44

I'm not saying that this is what you need for your problem, but sometimes when you want to superimpose graphics like this, you're looking for Inset:

Show[
g1,
Graphics@Inset[ 