# How can I fix an Inset plot to the top right corner of the main plot?

I want to export multiple plots and always have a small picture of another plot in the top right corner (as there is no data of the main plot in this region). However, it would be nice if I didn't have to set the position argument in Inset[] manually, as my y-values change for every plot. Thank you for any help! Below is an example of my code with the export part cut out off the For-loop. The issue is in the last line of the code, where I try to determine the position of the Inset-plot depending on the y-values, which is not working very well.

For[zz = 1, zz <= 4, zz++,
LargeRangePlotLargeM =
PlotRange -> All];
LargeRangePlotWithLargeM =
PlotLegends -> {"PPS", "RS", "PRS",  "GHZ"},
AxesLabel -> {"M", "FSA"},
PlotLabel -> StringForm["N = ", zz + 1],
PlotMarkers -> "OpenMarkers",  PlotRange -> All,
Epilog ->
Inset[LargeRangePlotLargeM, {750,
]


Simpler code with the same question might look like this:

p2 = Plot[Sin[x], {x, 0, \[Pi]}];
pcombined =
Plot[Sin[x], {x, 0, 2 \[Pi]}, Epilog -> Inset[p2, {5, 0.5}]]


How do I now define the position argument inside Inset in a way that it moves the plot to the top right corner without having to choose plot specific values manually?

• Welcome! Could you please add code for different (small) sample plots to clarify what you want to achieve? Adding code is usually extremely helpful for formulating answers. Dec 6, 2019 at 10:22
• Yes, of course: Dec 6, 2019 at 10:27
• Maybe Plot[Sin[x], {x, 0, 2 \[Pi]}, Epilog -> Inset[p2, {Right, Top}, {Right, Top}], ImageSize -> Large] ? Dec 6, 2019 at 10:41
• @camileri I get that the {Right, Top} (3rd argument) corner of the inset is placed at the {Right, Top} (2nd argument) corner of the main plot so the inset is fully visible. Dec 6, 2019 at 10:58
• @camileri Please try adjusting the PlotRange of the inset or adding some Padding. Dec 6, 2019 at 13:09

You can use Scaled and ImageScaled as position specifications. Since you want the full graphic including image padding included in the Inset, use ImageScaled[{1,1}] for the alignment point, and use Scaled[{1,1}] for the position so that the frame ticks show up at the top and right of the inset graphic. For example, here's a graphic with frame ticks:

p = Plot[Sin[x], {x, 0, Pi}, Frame->True, FrameTicks->All]


And here is how to inset the graphic using Epilog:

Plot[
Sin[x],
{x, 0, 2 Pi},
Epilog -> Inset[p, Scaled[{1,1}], ImageScaled[{1,1}]],
ImageSize -> Large
]


I would use four of the five possible arguments of Inset to position and scale the auxiliary plot. Like so:

aux = Plot[Sin[x], {x, 0, Pi}, Frame -> True, FrameTicks -> All];
Plot[Sin[x], {x, 0, 2 π},
Epilog -> Inset[aux, {Right, Top}, {Right, Top}, Scaled[{.4, .4}]]]


### Notes

• The 1st argument is, of course, the auxiliary plot.
• The 2nd argument tells Plot to put the auxiliary plot in the top-right corner of the main plot.
• The 3nd argument tells Plot to align the top-right corner of the the auxiliary plot at the position given by 2nd argument. Without this argument the auxiliary plot would be aligned so the its center was aligned at the position given by 2nd argument.
• The 4th argument gives indicates the scaling to give the auxiliary plot in the $$x$$ and $$y$$ directions. In this 40% in both directions.

### Update

The following is added to address concerns raised by the Rohit Namjoshi in a comment to this answer.

{Right, Top} is a simple and convenient form for the 2nd argument of Inset to get the inset placed in the upper-right corner, but sometimes you need more control which can be achieved by using Scaled. In the case you describe the following appears to work:

aux = Plot[Sin[x], {x, 0, π}];
Plot[Sin[x], {x, 0, 2 π},
PlotLabel -> "Test",
Epilog -> Inset[aux, Scaled[{1, 1}], {Right, Top}, Scaled[{.4, .4}]]]


• Thanks for the explanation. However, if I add PlotLabel -> "Test" then the inset gets clipped at the top. Looks like Top refers to the top of the image, not the top of the frame. Is there a way to align with the frame? Apr 27, 2020 at 17:29