Function Repository Resource:

DragZoomShow

Source Notebook

A version of Show that allows you to zoom in on a graphic using your mouse

Contributed by: Jon McLoone

ResourceFunction["DragZoomShow"][gr]

shows Graphics gr which can be zoomed using the mouse.

ResourceFunction["DragZoomShow"][Dynamic[range],gr]

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

Click and drag within the resulting plot to cause it to change the PlotRange.
Click in the plot to reset to the original PlotRange.
ResourceFunction["DragZoomShow"] only supports 2D graphics.

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["DragZoomShow"][ListPlot[Sin[Range[0, 2 Pi, 0.1]]]]
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, 25}, {0, 1}};
Column[{ResourceFunction["DragZoomShow"][Dynamic[range], ListPlot[Sin[Range[0, 2 Pi, 0.1]]], ImageSize -> 300], Dynamic[range]}]
Out[3]=

Options (1) 

All options for Graphics are supported:

In[4]:=
ResourceFunction["DragZoomShow"][ListPlot[Sin[Range[0, 2 Pi, 0.1]]], Background -> Yellow]
Out[4]=

Possible Issues (4) 

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

In[5]:=
ResourceFunction["DragZoomShow"][ListPlot[Sin[Range[0, 2 Pi, 0.1]]], ImageSize -> 100]
Out[5]=

Within Dynamic or Manipulate plot ranges automatically reset with each refresh:

In[6]:=
Manipulate[
 ResourceFunction["DragZoomShow"][
  ListPlot[Sin[f Range[0, Pi, 0.1]]]], {f, 1, 5}]
Out[6]=

Specify a range variable to prevent automatic resets:

In[7]:=
DynamicModule[{range},
 Manipulate[
  ResourceFunction["DragZoomShow"][Dynamic[range], ListPlot[Sin[f Range[0, Pi, 0.1]]]], {f, 1, 5}]]
Out[7]=

Zooming graphics with a fixed AxesOrigin may cause Axes not to be visible if the drag area does not cross the Axes:

In[8]:=
Options[ListPlot[Sin [Range[0, Pi, 0.1]]], AxesOrigin]
Out[8]=
In[9]:=
range = {{10, 25}, {0.5, 1}};
ResourceFunction["DragZoomShow"][Dynamic[range], ListPlot[Sin[Range[0, 2 Pi, 0.1]]]]
Out[10]=

Including AxesOrigin Automatic in the DragZoomShow will cause Axes to be reconsidered after a zoom action:

In[11]:=
range = {{10, 25}, {0.5, 1}};
ResourceFunction["DragZoomShow"][Dynamic[range], ListPlot[Sin[Range[0, 2 Pi, 0.1]]], AxesOrigin -> Automatic]
Out[12]=

The original graphic is not recomputed after a zoom action. This will cause a loss of sampling resolution:

In[13]:=
range2 = {{-0.01, 0.01}, {-0.01, 0.01}};
ResourceFunction["DragZoomShow"][Dynamic[range2], Plot[x Sin[1/x], {x, -1, 1}]]
Out[14]=

Increasing the sample rate much higher than is initially visible will help:

In[15]:=
range2 = {{-0.01, 0.01}, {-0.01, 0.01}};
ResourceFunction["DragZoomShow"][Dynamic[range2], Plot[x Sin[1/x], {x, -1, 1}, PlotPoints -> 600]]
Out[16]=

Use the ResourceFunction DragZoomPlot instead to recompute the contents after zoom:

In[17]:=
range2 = {{-0.01, 0.01}, {-0.01, 0.01}};
ResourceFunction["DragZoomPlot"][Dynamic[range2], x Sin[1/x], {x, -1, 1}]
Out[18]=

Publisher

Jon McLoone

Version History

  • 1.0.0 – 25 February 2022

Related Resources

License Information