# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Combine plots, enabling the creation of plots with two sets of axes and the merging of prologs and epilogs

Contributed by:
Lukas Lang

ResourceFunction["CombinePlots"][ works like Show, but can reorder labels/callouts and reposition frame axes. |

ResourceFunction["CombinePlots"] combines graphics expressions while trying to move labels and callouts to the front.

ResourceFunction["CombinePlots"] by default merges the primitives inside the Prolog and Epilog options.

ResourceFunction["CombinePlots"] can reposition frame axes, allowing the creation of plots with two *x*/*y* axes.

ResourceFunction["CombinePlots"] accepts the following options:

"CombineProlog" | True | whether to merge prologs |

"CombineEpilog" | True | whether to merge epilogs |

"AnnotationPattern" | (GraphicsGroup|Text)[___] | the pattern used to match annotations |

"AxesSides" | Automatic | on which side to put the frame axes |

ResourceFunction["CombinePlots"] effectively removes all expressions matching the pattern specified by "AnnotationPattern" and inserts them on top of the remaining primitives.

The default setting of "AnnotationPattern", (GraphicsGroup|Text)[___] matches the primitives generated from Label and Callout directives.

The option "AxesSides" accepts the following settings:

Automatic | do not move frame axes |

"TwoY" | move the vertical frame axis of the second plot to the right |

"TwoX" | move the horizontal frame axis of the second plot to the top |

"TwoXY" | move the bottom/left frame axes of the second plot to the top/right |

"MultiY" | use a separate vertical frame axis for each plot alternatingly adding to the left/right side |

"MultiX" | use a separate horizontal frame axis for each plot alternatingly adding to the bottom/top side |

"MultiXY" | use a separate pair of vertical and horizontal frame axes for each plot |

spec | use explicit settings for different plots |

"AxesSides" supports the same sequence specification as documented for Spacings.

For each plot, the setting can be one of the following:

Bottom | put horizontal frame axis to the bottom |

Top | put horizontal frame axis to the top |

Left | put vertical frame axis to the left |

Right | put vertical frame axis to the right |

side→Automatic | put the frame axis onto a separate axis on the specified side |

side→i | put the frame axis onto the axis with the specified index, counting from the inside |

{side_{x},side_{y}} | position both horizontal and vertical frame axes |

Directive[side_{x},side_{y}] | same as {side_{x},side_{y}} |

The option "AxesSides" settings "TwoY"/"TwoX"/"TwoXY" are effectively equivalent to settings of the form {2→*spec*}.

The settings "MultiY"/"MultiX" are effectively equivalent to settings of the form {{*side*_{1}→Automatic,*side*_{2}→Automatic}}, and "MutilXY" is a combination of the former two.

The sides for the frame axes of plots can also be specified by wrapping plots with Axes[*plot*,*sideSpec*], similar to how Item works for Grid.

The arguments of ResourceFunction["CombinePlots"] can be (nested) lists of plots, where each level can be wrapped in Axes[*plots*,*sideSpec*]. Deeper specifications override outer ones as necessary.

The setting of "AxesSides" applies to each argument of ResourceFunction["CombinePlots"] as one. Use Axes[…] type specifications for more granular control.

ResourceFunction["CombinePlots"] will use the frame axis of the first appropriate plot for each side. The values of the FrameLabel, FrameTicks, FrameStyle and FrameTicksStyle options are used to create the frame axis in the resulting plot.

ResourceFunction["CombinePlots"] effectively uses GeometricTransformation to rescale plot contents shown on secondary axes. Additional axes beyond the first axis per side are added using Inset.

ResourceFunction["CombinePlots"] sets CoordinatesToolOptions to enable extraction of coordinates from any of the axes. The format is {{{*side*_{x},*i*_{x}},{*side*_{y},*i*_{y}}}→{*x*,*y*},…}, with one entry for each unique horizontal/vertical axis combination. The *i*_{x},*i*_{y} indicate the index of the axis on the respective side.

If there are no labels/callouts and no prolog/epilog to combine, ResourceFunction["CombinePlots"][*args*] is equivalent to Show[*args*].

Combine two plots containing annotations:

In[1]:= |

Out[1]= |

If Show is used instead, the annotations of the first plot are covered by the contents of the second one:

In[2]:= |

Out[2]= |

Use CombinePlots to combine Prolog and Epilog contents:

In[3]:= |

Out[3]= |

Show only keeps the Prolog/Epilog of the first Graphics:

In[4]:= |

Out[4]= |

Use CombinePlots to create a plot with two vertical axes:

In[5]:= |

Out[5]= |

Merge arbitrary plots into plots with two *y* axes:

In[6]:= |

Out[6]= |

With the default setting "CombineProlog"→True, the graphics primitives of each Prolog are combined:

In[7]:= |

Out[7]= |

Only take the Prolog of the first Graphics, mimicking the behavior of Show:

In[8]:= |

Out[8]= |

With the default setting "CombineEpilog"→True, the graphics primitives of each Epilog are combined:

In[9]:= |

Out[9]= |

Only take the Epilog of the first Graphics, mimicking the behavior of Show:

In[10]:= |

Out[10]= |

The default setting of "AnnotationPattern" matches primitives generated for Callout and Label wrappers:

In[11]:= |

Out[11]= |

Bring only Text primitives to the front:

In[12]:= |

Out[12]= |

Bring only GraphicsGroup primitives to the front:

In[13]:= |

Out[13]= |

With the default setting "AxesSides"→Automatic, all plots share the same axes:

In[14]:= |

Out[14]= |

Use a secondary vertical axis for the second plot:

In[15]:= |

Out[15]= |

The different axes can have different ScalingFunctions:

In[16]:= |

Out[16]= |

Create a plot with a secondary horizontal axis:

In[17]:= |

Out[17]= |

Use both secondary *x* and *y* axes:

In[18]:= |

Out[18]= |

Use a separate vertical axis for each plot, and label each of them:

In[19]:= |

Out[19]= |

Put the contents of multiple plots on the secondary axis:

In[20]:= |

Out[20]= |

Move the vertical axis of all plots to the right:

In[21]:= |

Out[21]= |

Use Axes[*plot*,*sideSpec*] to specify the sides on which to put axes:

In[22]:= |

Out[22]= |

Put one plot on the secondary vertical axis, and another one on both the secondary horizontal and vertical axes:

In[23]:= |

Out[23]= |

- 2.1.1 – 21 June 2023
- 2.1.0 – 19 May 2023
- 2.0.2 – 27 September 2022
- 2.0.1 – 19 February 2021
- 2.0.0 – 20 December 2019
- 1.0.0 – 24 October 2019

This work is licensed under a Creative Commons Attribution 4.0 International License