1
$\begingroup$

Consider two plots, one of which will contain the other as an inset.

 inset = Plot[ x, {x, 1, 2}
  , Frame -> True
 ]
 A = Plot[ x^2 , {x, 1, 2} 
   , Epilog -> { Inset[ inset,  {1.5, 2.5},  {1, 1}, 0.5]}
 ] 

This code works perfectly with one limitation. Let's say I want to place the inset that spans from 1.5 to 1.9 in the scale of x-axis in the main panel while keeping the aspect ratio of the inset constant. To achieve this effect, I had to manually controls the fourth option, which scales the inset object. Is there anyway to automate this process?

$\endgroup$

1 Answer 1

2
$\begingroup$

From How to make Inset graphics maintain relative sizes when combined the sizing itself is easy if we rasterize the inset plot:

Manipulate[
 p1 = Plot[x, {x, 1, 2}, Frame -> True] // Rasterize;
 p2 = Plot[x^2, {x, 1, lim}, GridLines -> {{1.5, 1.9}}];
 size = (1.9 - 1.5)/(#2 - #) & @@ PlotRange[p2][[1]];
 Show[p2, Epilog -> {Inset[p1, {1.5, 2.5}, {0, 0}, Scaled[size]]}],
 {lim, 2, 5}
]

enter image description here

However dealing the the resizing of the non-rasterized Graphics is tricky. How should the tick labels outside the plot range be handled?

Using LLlAMnYP's functions from How to align coordinate systems of Inset and enclosing Graphics? we could write:

Manipulate[
 p1 = Plot[x, {x, 1, 2}, Frame -> True];
 p2 = Plot[x^2, {x, 1, lim}, GridLines -> {{1.5, 1.9}}];
 size = printerPointsPlotRange[p2][[1]]/realPlotRange[p2][[1]]*(0.4);
 Show[p2, Epilog -> {Inset[Show[p1, ImageSize -> size], {1.5, 2.5}, {Left, Bottom}, 
     Automatic]}],
 {lim, 2, 5}
]

enter image description here

This fits the inset including its tick labels neatly between the grid lines.

$\endgroup$

Your Answer

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

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