4
$\begingroup$

I want to insert a plot into another overlay plot. Here is my code:

xy1 = Table[{i, 2*i}, {i, 1, 10}];
xy2 = Table[{i, i^2}, {i, 1, 10}];
xy3 = Table[{i, i^3}, {i, 1, 10}];
p1 = ListPlot[xy1, FrameLabel -> {"x", "2x"}, 
   PlotStyle -> {Directive[Red, Dashed], Red}, 
   Frame -> {True, True, True, False}, ImagePadding -> 40, 
   FrameStyle -> {Automatic, Red, Automatic, Automatic}, 
   PlotMarkers -> {\[FilledCircle]}, Joined -> True, PlotRange -> All];
p2 = ListPlot[xy2, FrameLabel -> {"x", "", "", "x^2"},  
   PlotStyle -> Blue, Frame -> {False, False, False, True}, 
   ImagePadding -> 40, 
   FrameStyle -> {Automatic, Automatic, Automatic, Blue}, 
   FrameTicks -> {None, None, None, All}, 
   PlotMarkers -> \[FilledUpTriangle], Joined -> True, 
   PlotRange -> All];
p3 = ListPlot[xy3, FrameLabel -> {"x", "x^3"},  PlotStyle -> Green, 
  Frame -> {True, True, True, True}, PlotMarkers -> \[EmptyCircle], 
  Joined -> True, PlotRange -> All]
p12 = Overlay[{p1, p2}]
p123 = Graphics[{p12, Inset[p3]}]
p1232 = Graphics[{Rectangle[{0, 0}, {2, 2}, p12], 
   Rectangle[{0.3, 0.25}, {0.9, 0.9}, p3]}]
p1233 = Graphics[{First[p12], 
   Inset[p3, {5, 20}, Automatic, Scaled[.8]]}, PlotRange -> All, 
  AbsoluteOptions[p12]]

p12 is the overlay plot of p1 and p2, having two y axes. I want to insert p3 into p12.

I have read this post: How to insert a plot into another plot

And tried three ways to do the job, with the result figure represented as p123, p1232, and p1233 respectively. However, none of p123, p1232, and p1233 is successful:

p123:

enter image description here

p1232:

enter image description here

p1233:

enter image description here

Among them p123 and p1233 caused errors. P1232 is close to what I want, but still is very weird. I want to insert p3 into the blank area of p12. Can anyone please help?

$\endgroup$
3
  • $\begingroup$ Overlayed plots do not have a head Graphics they are an OverlayBox so this is why your code is failing. You can overlay more than two items so maybe that might be your best option. $\endgroup$ Nov 21, 2013 at 20:57
  • $\begingroup$ So is it possible to get double y axes (with different ticks) without overlay? $\endgroup$ Nov 21, 2013 at 21:01
  • $\begingroup$ Yes and you can search and find code for that but as shown in Kubas answer just put the inset in the relevant plot before you use overlay. $\endgroup$ Nov 21, 2013 at 21:26

1 Answer 1

13
$\begingroup$

There are couple of possibilities, for example:

  • via Overlay:

    Overlay[{p1, p2, Item[Show[p3, ImageSize -> 120], Alignment -> {-.5, .5}]}]
    

    enter image description here

  • via Epilog:

    Overlay[{p1, 
             Show[p2, Epilog -> {Inset[p3, Scaled[{.3, .7}], Center, Scaled@.5]}]
           }]
    

    enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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