Wolfram Research

Function Repository Resource:

PlotGrid

Source Notebook

Create a grid of plots with shared frame axes

Contributed by: Lukas Lang

ResourceFunction["PlotGrid"][{{plt1,1,},}]

arranges the given matrix of plots into a grid where adjacent plots share the frame ticks.

Details and Options

ResourceFunction["PlotGrid"] enables the creation of plots with shared frame ticks.
In ResourceFunction["PlotGrid"][{[plt1,1,},}], the plt1,1 can be Graphics expressions or Legended graphics expressions.
ResourceFunction["PlotGrid"] automatically hides frame ticks and labels on shared edges.
The graphics returned by ResourceFunction["PlotGrid"] is freely resizable, with all plots scaling appropriately.
ResourceFunction["PlotGrid"] accepts the same options as Graphics, with the following additions and changes:
AspectRatio Automatic aspect ratio of the plot grid
FrameLabel None outer frame labels, shared by all plots of the grid
FrameStyle Automatic the style of the outer frame and labels
ItemSize Automatic the size of the rows and columns
Spacings None the spacing to leave between the rows and columns
"ShowFrameLabels" Automatic which frame labels and ticks of the plots to show
With the default setting AspectRatioAutomatic, the aspect ratio is based on the geometric mean of the aspect ratios of the individual plots.
The labels specified by FrameLabel are drawn outside potential frame labels of the inner plots.
With the default setting FrameStyleAutomatic, the style is inherited from the first plot of the grid.
For ItemSize, the following settings can be given for an individual row/column:
Automatic use a constant default size
size use the specified size relative to other rows/columns
Scaled size item relative to the plot range
ImageScaled size item relative to the image size
Scaled [ s ] size by plot range, scaled by a factor s
ImageScaled [ s ] size by image size, scaled by a factor s
Offset [ size ] size in printers points
Offset [ offset , spec ] add offset printers points to the size specified by spec
For ItemSize, the setting Automatic is equivalent to specifying 1.
The settings Scaled and ImageScaled for ItemSize are equivalent to Scaled[1] and ImageScaled[1], respectively.
Specifying n for one item and Automatic for the remaining ones causes it to be n times as big as the rest.
When different types of scalings, e.g. Scaled and ImageScaled[2], are mixed, ResourceFunction["PlotGrid"] effectively tries to convert all specifications to pure size specifications with reasonable conversion factors.
At least one row and one column must use a relative ItemSize setting.
For Spacings, the following settings can be given for an individual gap between rows/columns:
size Size in printers points
Scaled [ s ] Fraction s of the plot grid width
The options ItemSize and Spacings support the full range of sequence specifications described in the respective documentation pages.
For "ShowFrameLabels", the following settings can be given for an individual plot:
Automatic show frame labels if there is no adjacent plot
Full show frame labels if there is a gap or no adjacent plot
All show all frame labels
True same as All
None show no frame labels
False same as None
{ hspec , vspec } use separate settings for the horizontal and vertical frame edges
{ { left , right } , { bottom , top } } use different settings for each side
{ side 1 spec 1 , } use the specified setting for the specified side
{ def , side 1 spec 1 , } use def for sides where no explicit value is given
The option "ShowFrameLabels" supports the same specifications to specify individual items as described in the documentation of ItemStyle.
In the setting for "ShowFrameLabels", Directive can be used instead of List to indicate that a specification should be interpreted as one instead of a sequence for different items.
In "ShowFrameLabels" settings, the sidei can be Left, Right, Top or Bottom.
The plti,j can be Null, in which case the corresponding grid space is left empty.
ResourceFunction["PlotGrid"] returns a Graphics expression or a Legended graphics expression.
ResourceFunction["PlotGrid"] sets CoordinatesToolOptions to enable extraction of coordinates from any of the plots. The format of the points is {{i,j},{x,y}}, where i,j are the indices of the plot and x,y the coordinates in the coordinate system of the plot.

Examples

Basic Examples

Create two plots sharing the y axis:

In[1]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[x, {x, 0, 1}, Frame -> True],
   Plot[x^2, {x, 0, 1}, Frame -> True]
   }
  }
 ]
Out[1]=

Create two plots stacked vertically:

In[2]:=
ResourceFunction["PlotGrid"][
 {
  {Plot[x, {x, 0, 1}, Frame -> True]},
  {Plot[x^2, {x, 0, 1}, Frame -> True]}
  }
 ]
Out[2]=

Specify a label for the shared axes:

In[3]:=
ResourceFunction["PlotGrid"][
 {
  {Plot[x, {x, 0, 1}, Frame -> True]},
  {Plot[x^2, {x, 0, 1}, Frame -> True]}
  },
 FrameLabel -> {"x axis", "y axis"}
 ]
Out[3]=

The legends of individual plots are combined:

In[4]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[
    {x, x^3},
    {x, 0, 1},
    Frame -> True,
    PlotLegends -> "Expressions"
    ]
   },
  {
   Plot[
    {x^2, x^4},
    {x, 0, 1},
    Frame -> True,
    PlotLegends -> "Expressions"
    ]
   }
  },
 FrameLabel -> {"x-axis", "y-axis"}
 ]
Out[4]=

Scope

Make a 2x2 grid of plots:

In[5]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[Sin[x], {x, -\[Pi], \[Pi]}, Frame -> True],
   Plot[Cos[x], {x, -\[Pi], \[Pi]}, Frame -> True]
   },
  {
   Plot[Sin[x]^2, {x, -\[Pi], \[Pi]}, Frame -> True],
   Plot[Cos[x]^2, {x, -\[Pi], \[Pi]}, Frame -> True]
   }
  }
 ]
Out[5]=

Omit one plot, creating an L-shaped grid:

In[6]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[Sin[x], {x, -\[Pi], \[Pi]}, Frame -> True],
   Plot[Cos[x], {x, -\[Pi], \[Pi]}, Frame -> True]
   },
  {Plot[Sin[x]^2, {x, -\[Pi], \[Pi]}, Frame -> True], Null}
  }
 ]
Out[6]=

Options

FrameLabel

Labels specified by FrameLabel are centered with respect to the full plot grid:

In[7]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[Sin[x], {x, -\[Pi], \[Pi]}, Frame -> True],
   Plot[Cos[x], {x, -\[Pi], \[Pi]}, Frame -> True]
   }
  },
 FrameLabel -> {"x axis", "y axis"}
 ]
Out[7]=

Label both individual frame axes and the whole group:

In[8]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[
    x, {x, 0, 1}, Frame -> True, FrameLabel -> {None, "y-axis 1"}
    ]
   },
  {
   Plot[
    x^2,
    {x, 0, 1},
    Frame -> True,
    FrameLabel -> {None, "y-axis 2"}
    ]
   }
  },
 FrameLabel -> {None, "y-axes"}
 ]
Out[8]=

FrameStyle

The setting for FrameStyle affects the frame labels shared by all plots:

In[9]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[
    x, {x, 0, 1}, Frame -> True, FrameLabel -> {None, "y-axis 1"}
    ]
   },
  {
   Plot[
    x^2,
    {x, 0, 1},
    Frame -> True,
    FrameLabel -> {None, "y-axis 2"}
    ]
   }
  },
 FrameLabel -> {None, "y-axes"},
 FrameStyle -> FontSize -> 16
 ]
Out[9]=

With the default setting FrameStyleAutomatic, the style is inherited from the first plot:

In[10]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[
    x,
    {x, 0, 1},
    Frame -> True,
    FrameLabel -> {None, "y-axis 1"},
    FrameStyle -> Red
    ]
   },
  {
   Plot[
    x^2,
    {x, 0, 1},
    Frame -> True,
    FrameLabel -> {None, "y-axis 2"},
    FrameStyle -> Red
    ]
   }
  },
 FrameLabel -> {None, "y-axes"}
 ]
Out[10]=

ItemSize

With the default setting ItemSizeAutomatic, all rows and columns have the same size:

In[11]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ]
 ]
Out[11]=

Make the middle row twice as high:

In[12]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 ItemSize -> {Automatic, {2 -> 2}}
 ]
Out[12]=

Size the rows and columns according to the plot ranges:

In[13]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 ItemSize -> Scaled
 ]
Out[13]=

Size the rows and columns according to the plot ranges, but make the third column half as wide:

In[14]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 ItemSize -> {{{Scaled}, Scaled[0.5]}, Scaled}
 ]
Out[14]=

Make the first row and column 100 printers points big:

In[15]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 ItemSize -> {{Offset[100]}, {Offset[100]}}
 ]
Out[15]=

The remaining space is dynamically filled by the remaining row and columns:

In[16]:=
Manipulate[
 ResourceFunction["PlotGrid"][
  Table[
   Graphics[
    Text[{i, j}],
    PlotRange -> 1.2 {{-j, j}, {-i, i}},
    Frame -> True
    ],
   {i, 3},
   {j, 3}
   ],
  ItemSize -> {{Offset[100]}, {Offset[100]}},
  ImageSize -> Dynamic[size]
  ],
 {size, 200, 600}
 ]
Out[16]=

Spacings

With the default setting SpacingsNone, no space is left between rows and columns:

In[17]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ]
 ]
Out[17]=

Separate plots by 20 printers points:

In[18]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> 20
 ]
Out[18]=

Separate plots by 10% of the total plot grid width:

In[19]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> Scaled[0.1]
 ]
Out[19]=

Only insert space between rows:

In[20]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> {None, Scaled[0.1]}
 ]
Out[20]=

With "ShowFrameLabels"Full, frame labels and ticks are shown if there is a gap:

In[21]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> {None, Scaled[0.1]},
 "ShowFrameLabels" -> Full
 ]
Out[21]=

“ShowFrameLabels”

With the default setting "ShowFrameLabels"Automatic, frame ticks and labels are only shown if there is no adjacent plot:

In[22]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> {None, 20}
 ]
Out[22]=

Labels are shown if a plot is omitted:

In[23]:=
ResourceFunction["PlotGrid"][
 ReplacePart[
  Table[
   Graphics[
    Text[{i, j}],
    PlotRange -> 1.2 {{-j, j}, {-i, i}},
    Frame -> True
    ],
   {i, 3},
   {j, 3}
   ],
  {2, 2} -> Null
  ],
 Spacings -> {None, 20}
 ]
Out[23]=

Show frame labels everywhere where there is a gap:

In[24]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> {None, 20},
 "ShowFrameLabels" -> Full
 ]
Out[24]=

Show no frame labels:

In[25]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> {None, 20},
 "ShowFrameLabels" -> None
 ]
Out[25]=

Show all frame labels:

In[26]:=
ResourceFunction["PlotGrid"][
 Table[
  Graphics[
   Text[{i, j}],
   PlotRange -> 1.2 {{-j, j}, {-i, i}},
   Frame -> True
   ],
  {i, 3},
  {j, 3}
  ],
 Spacings -> {None, 20},
 "ShowFrameLabels" -> All
 ]
Out[26]=

Possible Issues

PlotGrid assumes that plots have compatible plot ranges:

In[27]:=
ResourceFunction["PlotGrid"][
 {
  {
   Plot[x, {x, 0, 3}, Frame -> True],
   Plot[x^2, {x, 0, 3}, Frame -> True]
   }
  }
 ]
Out[27]=

Neat Examples

Use PlotGrid to create new types of compound plots:

In[28]:=
data =
  RandomVariate[
   BinormalDistribution[{0, 0}, {1, 2}, 0.6], 1000
   ];
ResourceFunction["PlotGrid"][
 {
  {
   Histogram[
    data[[All, 2]],
    20,
    BarOrigin -> Left,
    Frame -> {{True, False}, {False, True}},
    FrameTicks -> {{True, False}, {False, False}},
    PlotRange -> {Automatic, {-7, 7}},
    PlotRangePadding -> 0,
    GridLines -> {None, Automatic}
    ],
   ListPlot[
    data,
    Axes -> False,
    Frame -> {{False, True}, {False, True}},
    GridLines -> Automatic,
    PlotRangePadding -> 0,
    PlotRange -> {{-4, 4}, {-7, 7}}
    ]
   },
  {
   Null,
   Histogram[
    data[[All, 1]],
    20,
    Frame -> {{False, True}, {True, False}},
    FrameTicks -> {{False, False}, {True, False}},
    PlotRange -> {{-4, 4}, Automatic},
    PlotRangePadding -> 0,
    GridLines -> {Automatic, None}
    ]
   }
  },
 ItemSize -> {{1, 3}, {3, 1}},
 Frame -> True,
 FrameTicks -> False
 ]
Out[29]=

Resource History

Source Metadata