Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
As far as I know, the builtin mouse-zoom/pan/rotate/etc. only works for 3D graphics. There are domestic community solutions though, see these threads: manipulating plots in 2D, zooming in 2D, panning in 2D. –  István Zachar Mar 18 '13 at 11:53

1 Answer 1

up vote 9 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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