开发者

Zoom region and display as a subplot within plot

开发者 https://www.devze.com 2023-02-19 13:55 出处:网络
Is it possible to zoom into a region and display it as a subplot within the same plot? Here is my p开发者_C百科rimitive attempt at freehand graphics, to illustrate my question:

Is it possible to zoom into a region and display it as a subplot within the same plot? Here is my p开发者_C百科rimitive attempt at freehand graphics, to illustrate my question:

Zoom region and display as a subplot within plot

I can think of using Plot, and then Epilog, but then I get lost in the positioning and in giving the plot its own origin (When I try Epilog on Plot, the new plot lays on top of the old one, using the old one's origin).

Also, it would be nice if the positioning of the subplot can be input, as different curves have different "empty regions" that can be used to position the image.

I've seen this in several articles and I can do this in MATLAB, but I have no clue how to do it in mma.


Use Inset. Here's an example:

f[x_] = Sum[Sin[3^n x]/2^n, {n, 0, 20}];
x1 = x /. FindRoot[f[x] == -1, {x, -2.1}];
x2 = x /. FindRoot[f[x] == -1, {x, -1.1, -1}];
g = Plot[f[x], {x, x1, x2}, AspectRatio -> Automatic,
   Axes -> False, Frame -> True, FrameTicks -> None];
{y1, y2} = Last[PlotRange /. FullOptions[g]];
Plot[Sum[Sin[3^n x]/2^n, {n, 0, 20}], {x, -Pi, Pi},
 Epilog -> {Line[{
     {{x2, y2 + 0.1}, {-0.5, 0.5}}, {{x1, y2 + 0.1}, {-3.5, 0.5}},
     {{x1, y1}, {x2, y1}, {x2, y2 + 0.1}, {x1, y2 + 0.1}, {x1, 
       y1}}}],
   Inset[g, {-0.5, 0.5}, {Right, Bottom}, 3]},
 PlotRange -> {{-4, 4}, {-3, 3}}, AspectRatio -> Automatic]

Zoom region and display as a subplot within plot


And, borrowing from belisarius' code, you can also select the focus of your inset interactively by selecting a position at the x-axis:

imgsz = 400;
f[x_] := Piecewise[{{Sin@x, Abs@x > .1}, {Sin[100 x], Abs[x] <= 0.1}}];

Manipulate[
 Plot[f[x], {x, -3, 3}, PlotRange -> {{-3, 3}, {-2, 5}}, 
  ImageSize -> imgsz, 
  Epilog -> 
   Inset[Plot[f[y], {y, p[[1]] - .3, p[[1]] + 0.3}, PlotStyle -> Red, 
     Axes -> False, Frame -> True, ImageSize -> imgsz/3], {1.5, 3}]],
 {{p, {0, 0}}, Locator, Appearance -> None}]

or, if you also want to place the inset interactively:

Manipulate[
 Plot[f[x], {x, -3, 3}, PlotRange -> {{-3, 3}, {-2, 5}}, 
  ImageSize -> imgsz, 
  Epilog -> 
   Inset[Plot[f[y], {y, p[[1, 1]] - .3, p[[1, 1]] + 0.3}, 
     PlotStyle -> Red, Axes -> False, Frame -> True, 
     ImageSize -> imgsz/3], p[[2]]]],
 {{p, {{0, 0}, {1.5, 3}}}, Locator, Appearance -> None}]

EDIT

one more alternative based on dbjohn's question:

imgsz = 400;
f[x_] := Piecewise[{{Sin@x, Abs@x > .1}, {Sin[100 x], Abs[x] <= 0.1}}];

Manipulate[
 Plot[f[x], {x, -3, 3}, PlotRange -> {{-3, 3}, {-2, 5}}, 
  ImageSize -> imgsz, 
  Epilog -> 
   Inset[Plot[f[y], {y, p[[1]] - .3, p[[1]] + 0.3}, PlotStyle -> Red, 
     Axes -> False, Frame -> True, ImageSize -> imgsz/3], 
    Scaled[zw]]], {{p, {0, 0}}, Locator, 
  Appearance -> None}, {{zw, {0.5, 0.5}, "Zoom window"}, Slider2D}]

Zoom region and display as a subplot within plot


Just a kickstart:

imgsz = 400;
f[x_] := Piecewise[{{Sin@x, Abs@x > .1}, {Sin[100 x], Abs[x] <= 0.1}}];

Plot[f[x], {x, -3, 3}, PlotRange -> {{-5, 5}, {-5, 5}}, 
 ImageSize -> imgsz, Epilog ->
  Inset[Plot[f[y], {y, -.3, 0.3}, PlotStyle -> Red, Axes -> False, 
    Frame -> True, ImageSize -> imgsz/3], {3, 3}]]  

Zoom region and display as a subplot within plot


I find this an area in need of better built in tools. I have been working on this solution based on a demo here. I prefer to have the zoomed image and unzoomed image separated and as a bonus I added a presentable area where one could put relevant text or equations. For different functions the aspect ratio may need to be tweaked manually.

(f[x_] := x^2;
 ; xMin = -5; yMin = -5; xMax = 5; yMax = 5; 
 Manipulate[
  Grid[{{LocatorPane[{a}, 
      Plot[f[x], {x, xMin, xMax}, 
       PlotRange -> {{xMin, xMax }, {yMin, yMax}}, 
       ImageSize -> Medium, AspectRatio -> 1, AxesOrigin -> {0, 0}]], 
     Plot[f[x], {x, (a[[1]]) + xMin*mag, (a[[1]]) + xMax*mag}, 
      PlotRange -> {{(a[[1]]) + xMin*mag, (a[[1]]) + 
          xMax*mag}, {(a[[2]]) + yMin*mag, (a[[2]]) + yMax*mag}}, 
      ImageSize -> Medium, AspectRatio -> 1, AxesOrigin -> {0, 0}], 
     Item[StringForm["This is a suitable area to put any text.
       Value of A is :
       `1` ", a], Alignment -> {Left, Top}]}}, Frame -> All, 
   ItemSize -> All, 
   Spacings -> 5], {{a, {0, 0}}, {xMin, yMin}, {xMax, yMax}, Locator, 
   Appearance -> 
    Graphics[{Yellow, Opacity[.2], 
      Rectangle[Scaled[{.5 - (mag/2), .5 - (mag/2)}], 
       Scaled[{.5 + (mag/2), .5 + (mag/2)}]]}]}, {{mag, .5, 
    "Magnification"}, 0.01, 1, Appearance -> "Labeled"}])

Zoom region and display as a subplot within plot

(f[x_] := 
   Piecewise[{{Sin@x, Abs@x > .1}, {Sin[100 x], Abs[x] <= 0.1}}];
 ; xMin = -3; yMin = -3; xMax = 3; yMax = 3; 
 Manipulate[
  Grid[{{LocatorPane[{a}, 
      Plot[f[x], {x, xMin, xMax}, 
       PlotRange -> {{xMin, xMax }, {yMin, yMax}}, 
       ImageSize -> Medium, AspectRatio -> 1, AxesOrigin -> {0, 0}]], 
     Plot[f[x], {x, (a[[1]]) + xMin*mag, (a[[1]]) + xMax*mag}, 
      PlotRange -> {(*{(a[[1]])+xMin*mag,(a[[1]])+xMax*
        mag},*){(a[[2]]) + yMin*mag, (a[[2]]) + yMax*mag}}, 
      ImageSize -> Medium, AspectRatio -> 1, AxesOrigin -> {0, 0}, 
      Frame -> True], 
     Item[StringForm["This is a suitable area to put any text.
       Value of A is :
       `1` ", a], Alignment -> {Left, Top}]}}, Frame -> All, 
   ItemSize -> All, 
   Spacings -> 5], {{a, {0, 0}}, {xMin, yMin}, {xMax, yMax}, Locator, 
   Appearance -> 
    Graphics[{Yellow, Opacity[.2], 
      Rectangle[Scaled[{.5 - (mag/2), .5 - (mag/2)}], 
       Scaled[{.5 + (mag/2), .5 + (mag/2)}]]}]}, {{mag, .06, 
    "Magnification"}, 0.01, 1, Appearance -> "Labeled"}])

Zoom region and display as a subplot within plot

0

精彩评论

暂无评论...
验证码 换一张
取 消