8

I use GraphicsGrid to get a table of contour plots, where each of the plots is using its own scaling, so I want to include the bar legend for each of the plots. But if I do this, GraphicsGrid seems to ignore the place needed for the legend when calculating the grid item size, and the plots are scaled larger then grid cells.

MWE:

GraphicsGrid@
 Table[ContourPlot[ Cos[x]/k + Cos[ m y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
   ColorFunction -> "Rainbow", PlotLegends -> Automatic], {k, 1, 
   2}, {m, 1, 2}]

oversized items in GrpahicsGrid

Is there any better way to fix it rather then to set fixed plot size and to tune manually the ItemAspectRatio, like below?

GraphicsGrid[
 Table[ContourPlot[ Cos[x]/k + Cos[ m y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
   ColorFunction -> "Rainbow", PlotLegends -> Automatic, 
   ImageSize -> 100], {k, 1, 2}, {m, 1, 2}], ItemAspectRatio -> .6]

enter image description here

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Oleg Soloviev
  • 407
  • 2
  • 8

2 Answers2

3

It's a bug. I had a similar problem with GraphicsGrid, reported it, and received a reply from WRI tech support. I quote the relevant portion.

When you put the plots inside a GraphicsGrid, they are put inside individual insets. Unfortunately, there is a known issue with GraphicsGrid failing to determine the optimal size for graphics with insets.

So, I recommend you to avoid using plots along the GraphicsGrid.

Try Grid instead.

Grid @
  Table[
    ContourPlot[Cos[x]/k + Cos[m y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
      ColorFunction -> "Rainbow", PlotLegends -> Automatic],
    {k, 1, 2}, {m, 1, 2}]

grid

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • Thanks a lot! I would still prefer to use manual tuning because of not uniform heights of the bar legends with Grid method. – Oleg Soloviev Sep 08 '17 at 20:40
1

One option is to rasterize the plots before using GraphicsGrid

GraphicsGrid[
 Table[Rasterize[
   ContourPlot[Cos[x]/k + Cos[m y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
    ColorFunction -> "Rainbow", PlotLegends -> Automatic, 
    ImageSize -> 300]], {k, 1, 2}, {m, 1, 2}]]

this will produce enter image description here

M_Heddar
  • 11
  • 1