8

I'm using ListLinePlot inside a Manipulate function to display some data.

I've seen that Mathematica allows zooming and panning but it's either only for 3D plots or it just doesn't work with ListLinePlot.

I've also seen a thread here on Stackexchange for a custom function to zoom and pan but it doesn't seem to work when I apply it to ListLinePlot (error message "... is not a graphics primitive or directive").

So is there a solution I've missed to be able to zoom and pan a ListLinePlot?

Sulli
  • 2,185
  • 14
  • 28

1 Answers1

12

OK, the following works within a Manipulate-ListLinePlot environment:

   Clear[x0];
    Manipulate[


     Grid[{{ListLinePlot[
         Table[{x, Sin[x]}, {x, -2 \[Pi], 2 \[Pi], \[Pi]/6}],
         Epilog -> {{Blue, Opacity[0.1], 
            Rectangle[{x0 - \[Delta], -1.1}, {x0 + \[Delta], 1.1}]}}], 
        "Full Plot"},

       {ListLinePlot[Table[{x, Sin[x]}, {x, -2 \[Pi], 2 \[Pi], \[Pi]/6}], 
         Axes -> False, GridLines -> Automatic, AspectRatio -> 1, 
         Frame -> True, Filling -> Axis, 
         PlotRange -> {{x0 - \[Delta], x0 + \[Delta]}, {-1, 1}}], 
        "Zoom View"}}], 


      {{\[Delta], \[Pi]/2, "Zoom Level"}, 10^-10, 
      2 \[Pi]}, {{x0, 0, "Center"}, -\[Pi], \[Pi]}]

(* this code looks prettier in a notebook! *)

The output looks like this:

pan-zoom

You could easily set up two more sliders to control the y-axis pan and zoom, like so:

    Clear[x0, y0];
    Manipulate[

     Grid[{{ListLinePlot[
         Table[{x, Sin[x]}, {x, -2 \[Pi], 2 \[Pi], \[Pi]/6}],
         Epilog -> {{Blue, Opacity[0.1], 
            Rectangle[{x0 - \[Delta], y0 - \[Gamma]}, {x0 + \[Delta], 
              y0 + \[Gamma]}]}}], "Full Plot"},

       {ListLinePlot[Table[{x, Sin[x]}, {x, -2 \[Pi], 2 \[Pi], \[Pi]/6}], 
         Axes -> False, GridLines -> Automatic, AspectRatio -> 1, 
         Frame -> True, Filling -> Axis, 
         PlotRange -> {{x0 - \[Delta], x0 + \[Delta]}, {y0 - \[Gamma], 
            y0 + \[Gamma]}}], "Zoom View"}}],


     {{\[Delta], \[Pi]/2, "x Zoom Level"}, 10^-10, 2 \[Pi]},
     {{x0, 0, "x Center"}, -\[Pi], \[Pi]},
     {{\[Gamma], 0.5, "y Zoom Level"}, 10^-10, 1.1},
     {{y0, 0, "y Center"}, -1, 1}
     ]

Which looks like this:

enter image description here

You can easily fiddle the parameters to suit your aesthetic.

geordie
  • 3,693
  • 1
  • 26
  • 33