35
$\begingroup$

I would like to know whether there is a way to insert a plot into another plot. I would like to do some plot from a function an then, inside this plot, in the right down corner add a smaller plot of the same function covering a smaller region. I tried with Epilog but it's not possible that way, it gives me a fail

Thanks in advance!

$\endgroup$
1

4 Answers 4

28
$\begingroup$

...or do it dynamically so you can position the inset where you like it:

DynamicModule[{pt = Scaled[{0.5, 0.5}]},

 Plot[Sin[x], {x, 0, 2 Pi},
  PlotRange -> All, 
  Epilog -> {Dynamic[
     Locator[Dynamic[pt], 
      Plot[Cos[x], {x, 0, 2 Pi}, Background -> White, 
       ImageSize -> 150]]]}]]

enter image description here

$\endgroup$
4
  • 4
    $\begingroup$ 1+ I liked this Dynamic tip! Cool! $\endgroup$
    – Murta
    Nov 14, 2012 at 13:53
  • 2
    $\begingroup$ That is pretty nice, but is there a method by which you can read the coordinates after you are done with placing? $\endgroup$ Aug 2, 2018 at 14:16
  • $\begingroup$ After running in version 12.1.1, your code outputs the following error message: "Raw object \!(*FormBox[\"x\", TraditionalForm]) cannot be used as \ an iterator." $\endgroup$ Aug 13, 2020 at 4:26
  • 2
    $\begingroup$ @Alittlemouseonthepampas if you change the 2 xs in the second Plot function to anything else it works. So change the second Plot to Plot[Cos[y], {y, 0, 2 Pi}, Background -> White, ImageSize -> 150] $\endgroup$ Nov 2, 2020 at 0:29
26
$\begingroup$

You can use Inset for this

Building a new Graphics

gr1 = Plot[Sin[x]^2, {x, 0, 6 Pi}];
gr2 = Plot[Sin[x]^2, {x, 0, Pi}];

Graphics[{First[gr1], Inset[gr2, {15, -.6}, Automatic, Scaled[.4]]}, 
 PlotRange -> {Automatic, {-1, 1}}, AbsoluteOptions[gr1]]

Mathematica graphics

Using Epilog

Plot[Sin[x]^2, {x, 0, 6 Pi}, PlotRange -> {Automatic, {-1, 1}}, 
 Epilog -> {Inset[gr2, {15, -.6}, Automatic, Scaled[.4]]}]
$\endgroup$
4
  • 1
    $\begingroup$ Ha Yep, question solved! That's what I was looking for! And with Background->White option it covers the old plot if necessary! Thanks a lot! $\endgroup$
    – pablo
    Nov 14, 2012 at 10:02
  • $\begingroup$ For some reason, this method doesn't work on Mathematica 10.0.1! $\endgroup$
    – Mahdi
    Mar 17, 2015 at 22:57
  • $\begingroup$ @Mahdi It works for me. $\endgroup$
    – halirutan
    Mar 17, 2015 at 23:05
  • $\begingroup$ @halirutan: Probably related to this. I'm getting Axes::axes: "{{False,False},{False,False}} is not a valid axis specification. " and Ticks::ticks: "{Automatic,Automatic} is not a valid tick specification." using under Linux 64-bit. $\endgroup$
    – Mahdi
    Mar 17, 2015 at 23:12
17
$\begingroup$

You mean something like this?

p1 = Plot[Sin[x], {x, 0, 2 π}];
p2 = Plot[Sin[x], {x, 0, π}];

Show[Graphics[{Rectangle[{0, 0}, {1, 1}, p1], 
  Rectangle[{0.7, 0.4}, {1, 1}, p2]}]]

enter image description here

$\endgroup$
6
  • 1
    $\begingroup$ This was how it was done before Inset[] came along... $\endgroup$ Nov 14, 2012 at 9:56
  • $\begingroup$ @J.M. Ha, it means that it is a long time that I didn't have the need to do something like this. $\endgroup$
    – VLC
    Nov 14, 2012 at 9:57
  • 2
    $\begingroup$ (+1) very nice. I don't think i have seen this syntax before; any references? $\endgroup$
    – kglr
    Nov 14, 2012 at 10:02
  • 1
    $\begingroup$ @kguler, it was documented in old versions; apparently, when Inset[] became available in version 6, that piece of syntax was quietly redacted. $\endgroup$ Nov 14, 2012 at 10:28
  • 2
    $\begingroup$ This is really helpful because Inset[] does not allow you to specify positions (at least I didn't figure out) in terms of the corners of the figure. It is difficult to know the center position but the end-points are easy to know. $\endgroup$
    – preeti
    Sep 19, 2013 at 0:33
16
$\begingroup$

In addition to Inset etc., you can do this manually with the 2D Graphics editing tools.

First producing plots as halirutan did:

p1 = Plot[Sin[x], {x, 0, 2 π}]
p2 = Plot[Sin[x], {x, 0, π}]

Mathematica graphics

Click the first one to select it (orange border):

Mathematica graphics

Then Copy or Cut the object.

Next, double-click the second graphic to enter editing mode (gray border):

Mathematica graphics

And Paste the first graphic:

Mathematica graphics

You can then position and resize the inset graphic using the orange frame.
When you are done just click outside of the main gray frame.

$\endgroup$
4
  • $\begingroup$ Even easier, thanks a lot! $\endgroup$
    – pablo
    Nov 14, 2012 at 11:25
  • $\begingroup$ This is a good solution, but in the end I would like to have a code from where I can re-run if I want those specifications again. Any simple way of retrieving that information? $\endgroup$ Dec 27, 2012 at 13:37
  • $\begingroup$ @J.C.Leitão Sorry, I never saw your comment. I can't think of anything practical but that could be a good question; why don't you post it? (If you haven't done so already in the long time since you asked this.) $\endgroup$
    – Mr.Wizard
    Aug 14, 2013 at 22:54
  • $\begingroup$ hahah I love this one because of the simplicity, though it won't show any useful technique . $\endgroup$
    – Ying Zhang
    Aug 13, 2020 at 3:14

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.