dynamically updates graphics, but preserves the plot range and tooltips.
Details and Options
ResourceFunction["InteractiveGraphics"][graphics] will create a dynamic version of graphics that can be interacted with by zooming, panning and adding tooltip labels to objects in the plot.
The tooltip labels placed by ResourceFunction["InteractiveGraphics"] will always snap to objects, such as points and lines present in the graphic, ensuring that the coordinates are properly read out.
The following interactions are supported by default:
+ drag
pan the plot around
/ + drag
zoom into the plot
/ + vertical/horizontal drag
zoom only along the vertical/horizontal axis
Double‐click
reset plot range to default
Click on point/line/…
add a tooltip label/move the active tooltip label
Click on label/anchor
select the tooltip label
/ + click on point/line/…
add additional tooltip labels
/ + click on label
remove the tooltip label
Drag label anchor
move the tooltip around
/ + drag label anchor
move the tooltip smoothly along lines
Drag label
move the tooltip label around
Right-click + "Copy Tooltip Coordinates"
copy the list of tooltip coordinates to the clipboard
Right-click + "Convert to Static Graphics"
disable interactivity while keeping the current plot range and tooltips
The cursor is changed dynamically to indicate the current action, as indicated by the previous table.
By default, the modifier keys are , and on Windows and Linux, and , and on macOS, as indicated in the previous table.
ResourceFunction["InteractiveGraphics"] accepts the following options:
method to use to update the plot when zooming and panning
"GroupingStrategy"
"Color"
how to attempt to group the plot elements
The option LabelStyle is effectively applied to all tooltip labels and can be a list of any of the options supported by Framed.
ResourceFunction["InteractiveGraphics"] effectively uses the setting of CoordinatesToolOptions to generate the contents of the labels and the coordinates copied by the "Copy Tooltip Coordinates" context menu option.
The value of LabelingFunction is applied to the "DisplayFunction" setting of the CoordinatesToolOptions of the graphics expression before being displayed.
The "LabelPosition" option controls how the tooltip labels are placed in relation to the anchor points or the graphic.
The "LabelPositon" can be set to the following:
"Attached"
label moves with the anchor and is fixed to one of the four corners
"Free"
label moves with the anchor, but can be placed anywhere
"FreeScaled"
label does not move with the anchor and can be positioned anywhere
pos
position the list of labels at the given position
In "LabelPosition"→pos, pos can be a pair of numbers (with coordinates from 0 to 1), a symbolic position (Left,Right,Bottom,Top or Center) or a combination of the two.
"AllowInterpolation" controls whether the anchors snap to the corner points of lines and similar objects, or whether any position along the line should be allowed.
With the default setting "AllowInterpolation"⧴CurrentValue["OptionKey"], the snapping behavior can be changed by holding the / key while dragging the anchor around.
The "AllowedActions" option controls which types of interactions are supported. Possible values are:
The option "ModifierKeys" controls the modifier keys that need to be held down to perform certain interactions.
The option "ModifierKeys" needs to be set to a list of three values, where the values specify the keys necessary for zooming, panning and label creation, respectively.
For each entry in the setting for "ModifierKeys", a CurrentValue argument can be given or a list of multiple alternatives can be specified as opt1,opt2,…. In principle, any argument where CurrentValue[arg] returns True or False is supported, but not all cases will work as expected.
The option "LabelMoveMethod" controls how tooltip anchors are moved when the graphic in ResourceFunction["InteractiveGraphics"][Dynamic[graphics]] is dynamically updated.
The following settings can be used for "LabelMoveMethod":
The setting "LabelMoveMethod"→Automatic is equivalent to "Vertical" for labels annotating lines, but tries to follow a given point when it is annotated by determining the index of the annotated point and finding the new point at that index.
For all settings of "LabelMoveMethod", only points along the same object group are considered (determined by index), where the object groups are determined by the "GroupingStrategy" option.
When no suitable position for a label can be found, it is put into an orphaned state, indicated by a red anchor point. The label can be reassigned to an object by making it active and clicking on an object to move it there.
The following settings can be used for "ReplottingMethod":
For the setting of "ReplottingMethod" to have an effect, the first argument of ResourceFunction["InteractiveGraphics"] must be wrapped in Dynamic since the unevaluated form of the graphics is needed for it to work.
With the setting "ReplottingMethod"→Automatic, ResourceFunction["InteractiveGraphics"] attempts to automatically recreate the plot by passing an updated plot range to the plotting function in order to properly sample the plot range that is currently visible.
With the setting "ReplottingMethod"→func, the specified function is used to create the updated plot. For ResourceFunction["InteractiveGraphics"][Dynamic[expr],"ReplottingMethod"→func], func[expr][plotRange] is evaluated with the current plot range in the form {{xmin,xmax},{ymin,ymax}} whenever necessary.
The "GroupingStrategy" option controls how the contents of a plot are grouped together for the purposes of tooltip label movement.
When dragging a tooltip label anchor interactively, the anchor will only move to points of the same object. The grouping also affects the automatic label movement, as noted previously.
The following settings can be used for "GroupingStrategy":
use Annotation[prims,ResourceFunction["InteractiveGraphics"][Annotation]] as a grouping indicator
The grouping strategy effectively examines the structure of the graphics expression from the outer levels inward, and when the first nontrivial structure is found, the items of that structure are grouped. Such structures might, for example, be lists of several elements, items with an explcit color given or Annotation[…] wrappers.
ResourceFunction["InteractiveGraphics"]["FixAfterUndo"] can restore interactive functionality in certain situations involving the "Convert to Static Graphics" action, as explained in Possible Issues.
Move the label to the blue curve by clicking on it:
Move the label out of the way of the intersection by dragging it downward:
Zoom into the region around the label by holding and dragging:
Add a second label using +click (+click on macOS) on the orange curve:
Move the plot range slightly to the right by holding and dragging:
Estimate the position of the maximum of a curve by plotting it and adding a label to the highest points:
In[2]:=
Out[2]=
Ensure the labels are as close as possible to the peak by holding ( on macOS) and dragging them upward (the further above the cursor is, the more accurate this will be):
Extract the positions by choosing "Copy Tooltip Coordinates" from the context menu and pasting the result:
Scope (3)
Coordinates with rationals and roots are supported:
In[3]:=
Out[3]=
Inspect a two-axis plot created by the resource function CombinePlots:
In[4]:=
Out[4]=
Use on other types of plots:
In[5]:=
Options (22)
LabelStyle (2)
With the default setting LabelStyle→{}, the tooltip labels have a white background and a colored border:
In[6]:=
Out[6]=
Add some padding, rounded corners and a semi-transparent background:
Use NumberForm to display only two decimal digits:
In[9]:=
Out[9]=
LabelPosition (2)
With the default setting "LabelPosition" →"Attached", the tooltip labels are attached to the anchors at one corner:
In[10]:=
Out[10]=
Allow the labels to be moved freely, but still together with the anchors:
In[11]:=
Out[11]=
Allow the labels to be moved freely and make them fixed with respect to the plot:
In[12]:=
Out[12]=
Put all labels into the top-left corner:
In[13]:=
Out[13]=
AllowInterpolation (3)
With the default setting "AllowInterpolation"⧴CurrentValue["OptionKey"], tooltips snap to key points of objects unless the key ( key on macOS) is held while dragging:
In[14]:=
Out[14]=
Always allow any point along lines to be selected:
In[15]:=
Out[15]=
Never allow points along lines to be selected:
In[16]:=
Out[16]=
AllowedActions (2)
With the default setting "AllowedActions" →All, all types of interactions are enabled:
In[17]:=
Out[17]=
Only allow zooming and panning:
In[18]:=
Out[18]=
UseLinkHandCursor (2)
With the default setting "UseLinkHandCursor"→True, the cursor changes when over any object that is labeled:
In[19]:=
Out[19]=
Disable the cursor in case it is distracting or confusing:
In[20]:=
Out[20]=
ModifierKeys (1)
Swap the modifier keys for zooming and panning:
In[21]:=
LabelMoveMethod (3)
With the default setting "LabelMoveMethod"→Automatic, tooltip labels on lines are moved vertically and tooltips on points are kept at the same point:
In[22]:=
Move labels to the nearest possible position:
In[23]:=
Move labels only horizontally, which will likely not be possible for labels on points:
In[24]:=
ReplottingMethod (3)
With the default setting "ReplottingMethod"→None, the graphics are never reevaluated by InteracitveGraphics:
In[25]:=
Automatically attempt to replot the graphics for the appropriate plot range:
In[26]:=
Use a custom function to do the replotting:
In[27]:=
GroupingStrategy (2)
With the default setting "GroupingStrategy"→"Color", objects in the plot are grouped based on their color:
In[28]:=
Group everything together:
In[29]:=
Possible Issues (6)
For Dynamic[…] type arguments, a Graphics expression must still be returned:
In[30]:=
Out[30]=
Interactivity might be disabled if the functions can't be reloaded after a kernel restart:
In[31]:=
Out[31]=
Trying to undo the action of "Convert to Static Graphics" does not work properly, and interacting with the resulting object will break future interactive graphics:
In[32]:=
Out[32]=
Fix the errors by calling InteractiveGraphics["FixAfterUndo"]:
In[33]:=
Out[33]=
The axes might move out of the visible area when panning:
In[34]:=
This is caused by the setting AxesOrigin→{0,0} of the graphics expression:
In[35]:=
Out[35]=
Resetting the setting to Automatic fixes the issue:
In[36]:=
Alternatively, use a frame instead of axes:
In[37]:=
The setting for "ReplottingMethod" only takes effect for Dynamic type arguments:
In[38]:=
Wrap the first argument in Dynamic to enable replotting:
In[39]:=
When zooming into a plot with a highly oscillatory function, the original sampling might not be sufficient to properly resolve all details:
In[40]:=
Use"ReplottingMethod"→Automatic to automatically refine the plot: