Function Repository Resource:

DragZoomPlot

Source Notebook

A version of Plot which allows you to zoom into the plot using the mouse

Contributed by: Jon McLoone

ResourceFunction["DragZoomPlot"][f,{x,xmin,xmax}]

generates a plot of f as a function of x from xmin to xmax that can be zoomed into using the mouse.

ResourceFunction["DragZoomPlot"][Dynamic[range],f,{x,xmin,xmax}]

takes the setting to be the dynamically updated current value of range, with the value of range being reset if the plot is clicked.

Details and Options

Click and drag within the resulting plot to cause it to change the PlotRange.
Click in the plot to reset to the original PlotRange.

Examples

Basic Examples (1) 

Click and drag to select regions of a plot that you wish to see enlarged. Single-click to reset the zoom area:

In[1]:=
ResourceFunction["DragZoomPlot"][Sin[1/x] x, {x, -0.5, 0.5}, PlotPoints -> 100]
Out[1]=

Scope (1) 

The selected range can dynamically update a variable and the plot updates if the variable changes value:

In[2]:=
range = {{0, 4}, {0, 1}};
Column[{ResourceFunction["DragZoomPlot"][Dynamic[range], Sin[x], {x, 0, 6}, ImageSize -> 300], Dynamic[range]}]
Out[3]=

Options (1) 

All options for Plot are supported:

In[4]:=
ResourceFunction["DragZoomPlot"][Sin[1/x] x, {x, -0.5, 0.5}, Background -> Yellow]
Out[4]=

Possible Issues (2) 

You cannot use the mouse to resize the result of DragZoomPlot, so it is is necessary to use ImageSize to control the size:

In[5]:=
ResourceFunction["DragZoomPlot"][Sin[1/x] x, {x, -0.5, 0.5}, ImageSize -> 100]
Out[5]=

If you use DragZoomPlot within Dynamic or Manipulate, you should specify a range variable or the range will reset with each refresh:

In[6]:=
DynamicModule[{range},
 Manipulate[
  ResourceFunction["DragZoomPlot"][Dynamic[range], Sin[f x], {x, 0, 6}, ImageSize -> 300], {f, 1, 5}]]
Out[6]=

Publisher

Jon McLoone

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.1.0 – 18 May 2021
  • 1.0.0 – 10 January 2019

License Information