Edit
This doesn't work in general. Clearly I have the wrong idea. I'll deal with this when I have the time.
Notably, Alexey Popkov gets divergent results on Windows and if I change the AspectRatio
of the enclosing plot we get results that aren't right. Suggests the ratio of aspect ratios is wrong.
Maybe someone else can take this and do it right.
Heres the code all in one block, too:
{r, b} = {-0.25, 0.5};(*right,bottom of inset*)
{ll,
ur} = {{-0.03, 0}, {0.03,
1.1}};(*rectangle bounds*)
w = .25;(*inset width*)
f =
Function[x, -Sin[1/(.08 + Abs[x])]];(*function to plot*)
ginset =
Plot[f[x], {x, First@ll, First@ur},
PlotRange -> {Last@ll, Last@ur},
Axes -> False, Frame -> True, FrameTicks -> None];
gmain =
Plot[f[x],
{x, -0.5, 0.5}, PlotRange -> All,
Ticks -> None, PlotStyle -> Black];
{ar, iar} =
AspectRatio /. Options[#, AspectRatio] & /@ {ginset, gmain};
{prx, pry} = PlotRange@gmain;
{ppx, ppy} =
MapThread[
Replace[{
{{Scaled[p1_], Scaled[p2_]}, v_} :> (p1 + p2)*
EuclideanDistance @@ v,
{Scaled[p_], v_} :> p*EuclideanDistance @@ v,
{p_, _} :> p
}]@*List, {
PlotRangePadding /. Options[gmain, PlotRangePadding],
{prx, pry}
}];
h =
w*ar/iar*
Divide @@
(EuclideanDistance @@@ {pry, prx} + {ppy, ppx});
Show[
gmain,
Prolog -> {
Inset[ginset, {r, b}, {Right, Bottom}, w,
Background -> Lighter[Gray, 0.95]],
{FaceForm[Lighter[Gray, 0.95]], EdgeForm[LightGray],
Rectangle[ll, ur]}, {Gray, Dashed, Line[{{r, b}, ll}]},
{Gray, Dashed, Line[{{r, b + h}, {First@ll, Last@ur}}
]}
},(*
AspectRatio\[Rule]2,*)
ImageSize -> 150
]
Original posting
So I think this should do it for you.
We'll start with our basic plot set up, more or less as you had it except without that Prolog
and with a w
for the Inset
width:
{r, b} = {-0.25, 0.5};(*right,bottom of inset*)
{ll, ur} = {{-0.03, 0}, {0.03, 1.1}};(*rectangle bounds*)
w = .25;(*inset width*)
f = Function[x, -Sin[1/(.08 + Abs[x])]];(*function to plot*)
ginset =
Plot[f[x], {x, First@ll, First@ur},
PlotRange -> {Last@ll, Last@ur},
Axes -> False, Frame -> True, FrameTicks -> None];
gmain =
Plot[f[x],
{x, -0.5, 0.5}, PlotRange -> All,
Ticks -> None, PlotStyle -> Black];
Then find various plot properties we'll need. Here I get the aspect ratios of both the inset plot and the primary plot as well as what will become the total plot range of the main plot (the padding adjustment is necessary):
{ar, iar} =
AspectRatio /. Options[#, AspectRatio] & /@ {ginset, gmain};
{prx, pry} = PlotRange@gmain;
{ppx, ppy} =
MapThread[
Replace[{
{{Scaled[p1_], Scaled[p2_]}, v_} :> (p1 + p2)*
EuclideanDistance @@ v,
{Scaled[p_], v_} :> p*EuclideanDistance @@ v,
{p_, _} :> p
}]@*List, {
PlotRangePadding /. Options[gmain, PlotRangePadding],
{prx, pry}
}];
Then we can compute the Inset
height from the ratio of the AspectRatios
and the ratio of plot x range and plot y range in the main plot:
h =
w*ar/iar*
Divide @@
(EuclideanDistance @@@ {pry, prx} + {ppy, ppx});
Then apply show on this:
Show[
gmain,
Prolog -> {
Inset[ginset, {r, b}, {Right, Bottom}, w,
Background -> Lighter[Gray, 0.95]],
{FaceForm[Lighter[Gray, 0.95]], EdgeForm[LightGray],
Rectangle[ll, ur]}, {Gray, Dashed, Line[{{r, b}, ll}]},
{Gray, Dashed, Line[{{r, b + h}, {First@ll, Last@ur}}
]}
}
]
And if we try a bunch of numeric (this is crucial as I do no type checking to adjust for symbolics) AspectRatios
, in this case for {1./GoldenRatio, 1/3, 2/3}
:
Seems to work.
Dunno if there's an easier way, but I think this'll do for you at least.
(* the problem *)
is the1.05
chilling there? $\endgroup$Scaleds
andOffsets
and whatnot. $\endgroup$