1
$\begingroup$

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

enter image description here

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?

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

2 Answers 2

3
$\begingroup$

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]

enter image description here

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

enter image description here

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

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}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.