3
$\begingroup$

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 = 
  ListPlot[{ DeltaDeltaPPS2to5LargeM[[zz]],  
    DeltaDeltaRS2to5LargeM[[zz]], DeltaDeltaPRS2to5LargeM[[zz]], 
    DeltaDeltaGHZ2to5LargeM[[zz]]}, PlotMarkers -> "OpenMarkers", 
   PlotRange -> All];
 LargeRangePlotWithLargeM = 
  ListPlot[{DeltaDeltaPPS2to5[[zz]], DeltaDeltaRS2to5[[zz]], 
    DeltaDeltaPRS2to5[[zz]], DeltaDeltaGHZ2to5[[zz]]}, 
   PlotLegends -> {"PPS", "RS", "PRS",  "GHZ"},  
   AxesLabel -> {"M", "FSA"}, 
   PlotLabel -> StringForm["N = ``", zz + 1], 
   PlotMarkers -> "OpenMarkers",  PlotRange -> All, 
   Epilog -> 
    Inset[LargeRangePlotLargeM, {750, 
      2 * DeltaDeltaGHZ2to5[[1]][[2]][[2]]}]];
]

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?

$\endgroup$
10
  • 1
    $\begingroup$ 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. $\endgroup$
    – Yves Klett
    Dec 6, 2019 at 10:22
  • 2
    $\begingroup$ Yes, of course: $\endgroup$
    – camileri
    Dec 6, 2019 at 10:27
  • 1
    $\begingroup$ Maybe Plot[Sin[x], {x, 0, 2 \[Pi]}, Epilog -> Inset[p2, {Right, Top}, {Right, Top}], ImageSize -> Large] ? $\endgroup$ Dec 6, 2019 at 10:41
  • 1
    $\begingroup$ @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. $\endgroup$ Dec 6, 2019 at 10:58
  • 1
    $\begingroup$ @camileri Please try adjusting the PlotRange of the inset or adding some Padding. $\endgroup$ Dec 6, 2019 at 13:09

2 Answers 2

3
$\begingroup$

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]

enter image description here

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
]

enter image description here

$\endgroup$
1
$\begingroup$

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}]]]

plot

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}]]]

plot

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ Apr 27, 2020 at 17:29
  • $\begingroup$ @RohitNamjoshi. I have updated my answer to address your concern. $\endgroup$
    – m_goldberg
    Apr 27, 2020 at 23:15
  • $\begingroup$ Thank you for the update. $\endgroup$ May 1, 2020 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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