# Drawing Axes on top of a Grid of plots

I have a grid of density plots, for example like this toy example

plots = Table[DensityPlot[Cos[2 \[Pi] (n x^2 + m y^2)], {x, -1, 1}, {y, -1, 1}, ImageSize -> 150, PlotPoints -> 20], {n, -2, 2}, {m, -2, 2}];
plotgrid = Grid[plots, Frame -> All]


I want to add axes on top of the grid so it looks something like this

The problem is that the Grid is not a Graphics object, so my naive attempt didn't work, e.g. this gives an error:

Show[plotgrid, Graphics@Arrow@{Scaled[{-0.1, 0.5}], Scaled[{1.1, 0.5}]}]


Is there some simple way to treat the entire Grid object as a graphics object, or other solution to drawing axes or other Graphics objects on top of the Grid?

• Unlike Grid, GraphicsGrid returns a graphics object. Nov 22, 2021 at 13:36
• @GustavoDelfino I'm aware of that, unfortunately GraphicsGrid has a known bug where it doesn't play nicely with PlotLegends :(
– Kai
Nov 22, 2021 at 15:23

You can Rasterize Grid[plots] and use it with Inset as Prolog in Graphics:

Graphics[MapThread[{AbsoluteThickness[3], #, Arrowheads[Large], Arrow[#2]} &,
{{Red, Green, Blue},
{{{0, 0}, {1, 1}}, {{0, 1/2}, {1, 1/2}}, {{1/2, 0}, {1/2, 1}}}}],
Prolog -> Inset[Rasterize[Grid[plots, Frame -> All]], Automatic, Automatic, 1],
ImageSize -> Large]


Use .9 instead of 1 in the last argument of Inset[...] to get

• This is the solution I wound up going with, although rasterizing is really not ideal unfortunately.
– Kai
Nov 22, 2021 at 15:24

You could use "Overlay" but the result is more a "hack" and not really satisfying:

dx = 1; dy = 1;
axes = Graphics[{Thickness[0.01], Red, Arrow[{{-dx, 0}, {dx, 0}}],
Arrow[{{0, -dy}, {0, dy}}], Green,
Arrow[{{-dx, -0.95 dy}, {dx, 0.95 dy}}],
Text[Style["X", Red, Large], {0.95 dx, 0.05}],
Text[Style["Y", Red, Large], {0.050, 0.95 dy}],
Text[Style["Y=X", Green, Large], {0.85 dx, 0.95 dy}]},
ImageSize -> {850, 740}, PlotRange -> {{-0.9, 1}, {-1, 1}}];
Overlay[{plotgrid, axes}]