8
$\begingroup$

I am learning a little about Inset, but after trying all options, I can't get this to work. I need to inset separately made Graphics2D objects, into one main Graphics2D but have them show up there with same relative sizes they had on their own. I use the Axes of the main Graphics as measuring stick yard to check that this is true.

For example, if I make disk of radius 1, and rectangle of width 6, and then Inset them to main Graphics, I expect them to show there with same relative sizes. Disk will still have radius 1 and the rectangle will still have width 6, but now when looking at the main Axes.

Here is an example:

 g1 = Graphics[{LightGray, Disk[{0, 0}, 1]}, AspectRatio -> Automatic];

Mathematica graphics

.ps the plot above is same code, but added Axes->True to it for illustration. I do not use Axes in the separate Graphics. But use Axes in the main Graphics.

 g2 = Graphics[{Opacity[.5], Blue, Rectangle[{-3, -1}, {3, 1}]}, AspectRatio -> Automatic];

Mathematica graphics

Now I want to insert the disk at origin of main Graphics, and insert the rectangle at location (2,2). But this is what happens when I do

Graphics[
 {Inset[g1, {0, 0}],
  Inset[g2, {2, 2}]
  }, Axes -> True, PlotRange -> {{-6, 6}, {-4, 4}}, AspectRatio -> Automatic]

Mathematica graphics

Looking at the above, the disk now shows with radius 2, and the rectangle became square. (relative the Axes of the main graphics). What I'd like to get is this: (did this manually, by adjusting the image sizes one by one after inserting)

Mathematica graphics

I tried many options, but can't get the effect I want. Tried Scaled and changing aspect ratio setting, but did not help.

What is the correct way to inset graphics, so they show up with same axes limits/sizes as in the original graphics? but now will be relative to the main Graphics.

$\endgroup$
  • $\begingroup$ Strangely when I try to reproduce your code my rectangle do not become a square but stays a rectangle (with its correct aspect ratio.) (OSX10.9.5, v10.0.2) $\endgroup$ – SquareOne Feb 8 '15 at 17:21
  • $\begingroup$ In your graphic, the final disk radius is not 2 but 3. $\endgroup$ – SquareOne Feb 8 '15 at 17:23
5
$\begingroup$

This seems to be what you want:

Graphics[
  MapThread[
    Inset[##, {0, 0}, #2 - # & @@ First@PlotRange@#] &,
    {{g1, g2}, {{0, 0}, {2, 2}}}
  ],
  Axes -> True,
  PlotRange -> {{-6, 6}, {-4, 4}}
]

enter image description here

For precision I believe one will need to set PlotRangePadding -> 0 and ImagePadding -> 0 in each of the inset Graphics.

$\endgroup$
  • $\begingroup$ thanks, but this is not really what I meant. You just used the g1 axes instead of the master graphics axes I want to combine many graphics into. May be my example was too simple. I'll add g2 to make it more clear what I mean. i.e. I'll have Disk and Rectangle, each separate graphics2D, then want to combine them into one Graphics, using Inset, and have them come out with same relative sizes as each originally. Will update my question soon to make this clear. in few minutes... $\endgroup$ – Nasser Feb 8 '15 at 11:43
  • $\begingroup$ @Nasser Looking forward to your update... $\endgroup$ – Mr.Wizard Feb 8 '15 at 11:44
  • $\begingroup$ @Nasser I took another shot at this, hopefully with a bit more success. $\endgroup$ – Mr.Wizard Feb 8 '15 at 12:48
  • $\begingroup$ thanks, yes, this is exactly what I wanted. You are right, PlotRangePadding -> 0 is needed in the separate graphics for more accurate positioning. I'll test it with more graphics now as well. $\endgroup$ – Nasser Feb 8 '15 at 12:53
  • $\begingroup$ Did not know that PlotRange can be used as a function (PlotRange[somegraphics]) ... :) Any refs ? $\endgroup$ – SquareOne Feb 8 '15 at 18:26
0
$\begingroup$
g1 = Graphics[{LightGray, Disk[{0, 0}, 1]}, AspectRatio -> Automatic];
g2 = Graphics[{Opacity[.5], Blue, Rectangle[{0, 0}, {1, .5}]}, AspectRatio -> Automatic];
Graphics[
{
 Inset[g1, {0, 0}, {0, 0}, Scaled[{.35, 1}]],
 Inset[g2, {0, 2}, {0, 0}, Scaled[{.35, 1}]]
},
 Axes -> True,
 PlotRange -> {{-6, 6}, {-4, 4}},
 AspectRatio -> Automatic
]
$\endgroup$
  • $\begingroup$ Welcome to Mathematica Stack Exchange. Unfortunately this is not the result that Nasser desires; here both objects appear with the same width whereas the rectangle should be much wider. $\endgroup$ – Mr.Wizard Feb 8 '15 at 19:22
  • $\begingroup$ ....Rectangle[{0, 0}, {1, .333}]}, ..... Inset[g1, {0, 0}, {0, 0}, Scaled[{.175, 1}]], Inset[g2, {-1, 1}, {0, 0}, Scaled[{.35 + .175, 1}]] ..... $\endgroup$ – dofuuu Feb 8 '15 at 21:30

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.