Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This code produces two identical images, despite the difference in the rectangles:

r1 = Rectangle[{-1, -1}, {1, 1}];
r2 = Rectangle[{-1, -1}, {0, 1}];
f[img_] := 
 Show[ParametricPlot3D[{{t, 0, 0}, {0, t, 0}, {0, 0, t}}, {t, -1, 1}],
     Show[Graphics[img], ImageSize -> Large], {0.25, 0, 0}, Automatic,

If I do {Automatic,ImageScaled[0.25]}, I am told that it should be a pair of numbers, or a scaled or offset form.

I want the height of the inset to be 25% of the overall graphic, and the width to be scaled accordingly. I want it to work for any img, not just the two rectangles I gave; my actual use case involves .gif files. How do I make it so that f[r1] and f[r2] show different insets, i.e., inset the rectangles shown by Show[r1] and Show[r2], with the height scaled to 25% of the total height of the image?

share|improve this question
up vote 2 down vote accepted

I'm not exactly sure what the output should look like, but I think part of the problem lies in the way you have described Inset, from the documentation:

represents an object obj inset in a graphic.

Note that your code does not have a graphic into which your Inset is, well, inset.

If we redefine f:

f[img_] := 
 Show[ParametricPlot3D[{{t, 0, 0}, {0, t, 0}, {0, 0, t}}, {t, -1, 1}],
   Epilog -> {Inset[
     Show[Graphics[img], ImageSize -> Large], {0.5, 0.5}, Automatic, 

We now get a plot that is at least responsive to the different shapes of r1 and r2

Mathematica graphics

Mathematica graphics

I needed to change your {0,0,0} to {0.5,0.5} because Epilog expects Graphics not Graphics3D and Inset is expected scaled coordinates, so the center of the image is {0.5,0.5}. ImageScaled[...] can be manipulated to change the relative size of the inset and I think what you're left with now is tweaking these parameters to get the image where you want on the plot.

share|improve this answer

See if this will work. It should, since you can get the ImageDimensions of an imported GIF. Note: I changed r1, r2 to Graphics. You can apply f to Graphics or to Image.

r1 = Graphics@Rectangle[{-1, -1}, {1, 1}];
r2 = Graphics@Rectangle[{-1, -1}, {0, 1}];
f[img_] := 
  With[{g = ParametricPlot3D[{{t, 0, 0}, {0, t, 0}, {0, 0, t}}, {t, -1, 1}]},
       Inset[img, {0.25, 0, 0}, Automatic, 
        ImageScaled[ 0.25 #/Last[#] &[ImageDimensions[img]/ImageDimensions[g]] ]

Mathematica graphics

Caveat: ImageScaled depends on the dimensions of the enclosing image. They can change if the output of f is displayed with other things. For instance, I'm not sure how to handle {f[r1], f[r2]}. Perhaps one could scale by ImageDimensions@Rasterize[{f[r1], f[r2]}], but you'd have to know those dimensions before f was executed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.