Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

There's this gap inside Grid which I cannot seem to remove. Perhaps someone knows how.

blocks = Table[{Graphics[{Darker@Green, Rectangle[{0, 0}, {1000, 200}]}]}, {i, 6}]
myGrid = Grid[blocks, Spacings -> {0, 0}, Frame -> All]

At 100% resolution.

All nice and even at 100% zoom. Now if I zoom in on this to say 640% inside the notebook: At 640% resolution

Extra space appears below each item as shown. The orange box received by clicking on the graphics suggests that Grid is responsible for the gap but Spacings have already been set to 0. Ssing Magnify will also cause the issue to appear at lower zoom rates. Does anyone know a fix for this in Grid? Or do I have to resort to GraphicsGrid which doesn't have this issue.

EDIT: This is the result produced from using the code as provided by Mike's answer (copy and pasted). Mike And this one with it magnified so the code can be included in the screenshot. magnified

I'm going to re-emphasize that this issue only occurs when I zoom in far enough or use Magnify.

Edit2: I'm on Mathematica 9.0.0

share|improve this question
    
Add PlotRangePadding -> 0, ImagePadding -> 0 to your Graphics code –  Mike Honeychurch Jul 7 at 1:55
    
That doesn't seem to cut it. I still get the white space after I zoom in (though it does make the problem more obvious) –  Jonie Jul 7 at 1:59
    
please post a pic and the exact code that you used. –  Mike Honeychurch Jul 7 at 2:50
    
I can't reproduce this in Version 9.0.1.0 (Windows 8). But I suspect it might have something to do with the default setting of GridFrameMargins. `The default setting is GridFrameMargins->{{0.4,0.4},{0.5,0.5}}. –  kguler Jul 7 at 2:53
1  
Kind of an issue if you are using the Magnify variant of the EMF bar chart export hack... Oh wait... –  Verbeia Jul 7 at 13:46

5 Answers 5

I can reproduce the problem described by OP in Mathematica 9.0.1 on Windows 8.1.

By using FrontEnd`UndocumentedBoxInformationPacket to check the displayed Boxes' layout in the FrontEnd, I wildly guess that the cause of the problem might be hiding in the FrontEnd layout engine (of only the Windows version maybe?). If it's true, then there might be nothing we users can do with it...

Here is my experiment and observation. I extracted the layout information and redrew them in a more clarified way (click image to see it larger; the code is attached afterward). In the screenshot, the upper green grid and the left column in this panel are corresponding to Grid, and the lower grid and the right column are to GraphicsGrid:

demonstration of a possible bug from Grid or FrontEnd layout engine

blocks = Table[{Graphics[{Darker@Green, Rectangle[{0, 0}, {1000, 200}]}, PlotRangePadding -> 0, PlotRangeClipping -> True, ImagePadding -> 0]}, {i, 2}];

myGrid      = Grid[blocks, Spacings -> {0, 0}, Frame -> All];
myGraphGrid = GraphicsGrid[blocks, Spacings -> {0, 0}, Frame -> All, PlotRangePadding -> 0];

gridNB      = myGrid // ToBoxes // Notebook[{Cell[BoxData[#], CellMargins -> 0, CellFrameMargins -> 0]}, ShowCellBracket -> False, Saveable -> False, WindowClickSelect -> False, Selectable -> False, WindowFrame -> "ThinFrame", WindowElements -> {}, WindowFrameElements -> {}, WindowMargins -> {{10, Automatic}, {Automatic, 10}}, WindowSize -> {200, 300}] & // NotebookPut;
graphGridNB = myGraphGrid // ToBoxes // Notebook[{Cell[BoxData[#], CellMargins -> 0, CellFrameMargins -> 0]}, ShowCellBracket -> False, Saveable -> False, WindowClickSelect -> False, Selectable -> False, WindowFrame -> "ThinFrame", WindowElements -> {}, WindowFrameElements -> {}, WindowMargins -> {{10, Automatic}, {Automatic, 320}}, WindowSize -> {200, 300}] & // NotebookPut;

{mgnf1, mgnf2} = 100 CurrentValue[#, Magnification] & /@ {gridNB, graphGridNB};
mgnfController1 = Row[{HorizontalGauge[Dynamic[mgnf1, (SetOptions[gridNB, Magnification :> #/100.]; mgnf1 = #) &], {10, 1000}], Style[Row[{Dynamic[mgnf1], "%"}], 20]}];
mgnfController2 = Row[{HorizontalGauge[Dynamic[mgnf2, (SetOptions[graphGridNB, Magnification :> #/100.]; mgnf2 = #) &], {10, 1000}], Style[Row[{Dynamic[mgnf2], "%"}], 20]}];

Clear[BRtoPolygon]
BRtoPolygon[boundingRect_, zCoord_: RandomReal[]] := Outer[List, ##] & @@ MapAt[Minus, boundingRect\[Transpose], 2] // MapAt[Reverse, #, 2] & // ArrayPad[Flatten[#, 1], {0, {0, 1}}, zCoord] & // Polygon

gridLayout = DynamicModule[{bips, layer, zCoordRule, hueRule},
            Dynamic[
                Module[{Graphics3DTemp, zCoord, hue},
                    bips = MathLink`CallFrontEnd[FrontEnd`UndocumentedBoxInformationPacket[#]] &[gridNB];
                    layer = bips //. {FE`BoxType -> bt_, FE`Position -> pos_, FE`BoundingRectangle -> br_, __, FE`Children -> chd_} :>
                                        {
                                            bt,
                                            If[And @@ (NumericQ /@ Flatten[{pos, br}]), 
                                                Function[{pts, zCoord}, 
                                                        Graphics3DTemp[{
                                                                        FaceForm[Hue[hue, .1, 1]], EdgeForm[Hue[hue, 1, .7]], BRtoPolygon[pts, zCoord], 
                                                                        If[MemberQ[{"GridBox", "GraphicsBox", "RectangleBox"},bt],
                                                                           Text[Style[bt, 15], Append[{1, -1} Last[pts], zCoord]], {}]
                                                                       }]
                                                        ][pos + # & /@ br, zCoord],
                                                Hold[Sequence[]]],
                                            chd
                                        } /. {bt_String, Hold[Sequence[]], chd_} :> Hold[Sequence[]] // ReleaseHold;
                    zCoordRule = Map[Function[{pos}, pos -> (layer[[##]] & @@ pos /. zCoord -> Length[pos])], Position[layer, Graphics3DTemp[__]]];
                    hueRule = Thread[# -> Rescale[Range[Length@#]]] &@Position[layer, hue];
                    ReplacePart[ReplacePart[layer, zCoordRule], hueRule] /. Graphics3DTemp -> Graphics3D // Cases[#, g_Graphics3D :> g, ∞] & // Rest //
                        Show[#,
                                Axes -> True, AxesStyle -> Blue, AxesLabel -> (Style[#, 17, Italic, Blue, Bold] & /@ {"width", "height", "layer"}),
                                Boxed -> False, ViewVertical -> {-1, 0, 0}, ViewPoint -> 1000 {-.9, -1, .5}, 
                                BoxRatios -> {Automatic, Automatic, 400}, ImageSize -> 700, Lighting -> {{"Ambient", White}}] &
                    ],
                UpdateInterval -> 0]
            ];

graphGridLayout = DynamicModule[{bips, layer, zCoordRule, hueRule},
            Dynamic[
                Module[{Graphics3DTemp, zCoord, hue},
                    bips = MathLink`CallFrontEnd[FrontEnd`UndocumentedBoxInformationPacket[#]] &[graphGridNB];
                    layer = bips /. e : {FE`BoxType -> "InsetBox", FE`Position -> pos_, __, FE`Children -> {{FE`BoxType -> "GraphicsBox", __}}} :> ReplacePart[e, {6, 2, 1, 2, 2} -> pos] //.
                                    {FE`BoxType -> bt_, FE`Position -> pos_, FE`BoundingRectangle -> br_, __, FE`Children -> chd_} :>
                                        {
                                            bt,
                                            If[And @@ (NumericQ /@ Flatten[{pos, br}]), 
                                                Function[{pts, zCoord}, 
                                                        Graphics3DTemp[{
                                                                        FaceForm[Hue[hue, .2, 1]], EdgeForm[Hue[hue, 1, .7]], BRtoPolygon[pts, zCoord], 
                                                                        If[MemberQ[{"InsetBox", "GraphicsBox", "RectangleBox"}, bt],
                                                                           Text[Style[bt, 15], Append[{1, -1} First[pts], zCoord]], {}]
                                                                       }]
                                                        ][pos + # & /@ br, zCoord],
                                                Hold[Sequence[]]],
                                            chd
                                            } /. {bt_String, Hold[Sequence[]], chd_} :> Hold[Sequence[]] // ReleaseHold;
                    zCoordRule = Map[Function[{pos}, pos -> (layer[[##]] & @@ pos /. zCoord -> Length[pos] + Mean[pos[[1 ;; -1]]])], Position[layer, Graphics3DTemp[__]]];
                    hueRule = Thread[# -> Rescale[Range[Length@#]]] &@Position[layer, hue];
                    ReplacePart[ReplacePart[layer, zCoordRule], hueRule] /. Graphics3DTemp -> Graphics3D // Cases[#, g_Graphics3D :> g, ∞] & // Rest //
                        Show[#,
                                Axes -> True, AxesStyle -> Blue, AxesLabel -> (Style[#, 17, Italic, Blue, Bold] & /@ {"width", "height", "layer"}),
                                Boxed -> False, ViewVertical -> {-1, 0, 0}, ViewPoint -> 1000 {-.9, -1, .5}, 
                                BoxRatios -> {Automatic, Automatic, 700}, ImageSize -> 700, Lighting -> {{"Ambient", White}}] &
                    ],
                UpdateInterval -> 0]
            ];

panel = Grid[{{mgnfController1, mgnfController2},
              {gridLayout, graphGridLayout}},
             Frame -> All, Alignment -> {Left, Top}] // ToBoxes //
          Notebook[{Cell[BoxData[#], "Output", CellMargins -> 0, CellFrameMargins -> 0]}, ShowCellBracket -> False, Saveable -> False, WindowElements -> {}, WindowFrame -> "ThinFrame", WindowFrameElements -> {"CloseBox"}, WindowMargins -> {{220, Automatic}, {Automatic, 10}}, WindowSize -> All] & // NotebookPut;
share|improve this answer

I was able to reproduce the effect (in version 7 under Windows) but I don't yet fully understand it. Partly it appears when you have automatically-resizing Graphics objects contained in a Grid object that is wider than the Notebook window. For example, using this code:

blocks = Table[{Graphics[{Darker@Green, Rectangle[{0, 0}, {1000, 200}]}, 
     ImagePadding -> 0, PlotRangePadding -> 0]}, {i, 6}];

myGrid = Grid[blocks, Spacings -> {0, 0}, Frame -> All];

Style[myGrid, Magnification -> 3]

And in a Notebook window that is easily able to contain the Graphic I get a solid green area with only fine black dividers:

enter image description here

However, if I resize the Notebook window to a narrower-than-graphic size I see this:

enter image description here

However magnification also plays a role here. Using a fixed ImageSize, which prevents resizing, combined with magnification shows a gap even when the Notebook is plenty wide to contain it:

Style[
 myGrid /. g_Graphics :> Show[g, ImageSize -> 50],
 Magnification -> 4
]

enter image description here

share|improve this answer

Magnify 10 times or more, ...

The basic idea is to use a GraphicsGrid

blocks = Table[{Graphics[{Darker@Green, Rectangle[{0, 0}, {100, 20}]},
  ImagePadding -> None, PlotRangePadding -> None]}, {i, 6}]

myGrid = GraphicsGrid[blocks, Spacings -> Scaled@.0, Frame -> All]

The options differ slightly for GraphicsGrid, see DocumentationCenter

Edit1: More workarounds... Another approach is to "Rasterize" Graphics output or to "Pane" it

    blocks = Table[{Graphics[{Darker@Green, Rectangle[{0, 0}, {1, .2}]}, 
         ImagePadding -> None, PlotRangePadding -> None]}, {i, 6}] // 
      Map[Rasterize, #, {2}] &
    myGrid = Grid[blocks, Spacings -> {0, 0}, Frame -> All, 
      FrameStyle -> {Blue, AbsoluteThickness@3}]

    blocks = Table[{Graphics[{Darker@Green, Rectangle[{0, 0}, {1, .2}]}, 
         ImagePadding -> None, PlotRangePadding -> None]}, {i, 6}] // 
      Map[Pane, #, {2}] &
    myGrid = Grid[blocks, Spacings -> {0, 0}, Frame -> All, 
      FrameStyle -> {Blue, AbsoluteThickness@3}]

In simple cases setting grid's "Background" to Darker@Green is an acceptable workaround.

share|improve this answer
1  
Thanks hieron, did mention this in my original post that GraphicsGrid does work and is indeed a workaround for the given example. Though I would much prefer having a grid due to the flexibility in size it provides. –  Jonie Jul 7 at 22:37
blocks = Table[{Graphics[{Darker@Green, 
     Rectangle[{0, 0}, {1000, 200}]}, PlotRangePadding -> 0, 
    ImagePadding -> 0]}, {i, 6}]
myGrid = Grid[blocks, Spacings -> {0, 0}, Frame -> All]

enter image description here

If you are still seeing whitespace as per your comment then please post a pic and the exact code that you used.

Edit

As per reply to Mr Wizard. At the max (max of widow element) magnification of 300% I get

enter image description here

Using Magnify of 6 (600%) I still do not see a problem:

enter image description here

I'm on OS X 10.9.4 and Mma 9.0.1

share|improve this answer
    
Note that the OP says: All nice and even at 100% zoom. Now if I zoom in on this to say 640% inside the notebook: so you must zoom to (theoretically) see the problem. I do see the problem using version 7. –  Mr.Wizard Jul 7 at 3:14
    
Okay, despite the OP's statement I don't think that is a factor. I'm starting on an answer now. Correcting myself a second time it seems magnification is a factor after all. Gah. I'm too tired! –  Mr.Wizard Jul 7 at 3:21
    
I tried it at 300% before posting the answer. –  Mike Honeychurch Jul 7 at 3:28
    
please try the examples in my answer; it may not affect your system. –  Mr.Wizard Jul 7 at 3:31
2  
@Mr.Wizard as per above when applying Magnify (which is same as Style[...,Magnification->..], which OP says is a problem, I see no problem. No more time to spend on this since for me it is a non issue. A late for lunch –  Mike Honeychurch Jul 7 at 3:37

I did my investigations with V9.0.1 running on OS X. My findings may be (probably are) platform dependent.

First I eliminated the framing from the problem space by writing this code.

blocks =
  With[{size = {5, 1}},
    Table[
      Graphics[{
        {EdgeForm[Black], FaceForm[Transparent], Rectangle[{0, 0}, size]},
        {Darker@Green, 
           Scale[Rectangle[{0, 0}, size], .95 {1., 1. - size[[2]]/size[[1]]}]}},
        PlotRangePadding -> None],
      {2}]];

This gives good results over a wide range of magnifications.

Magnify[Column[blocks, Spacings -> -.14], 1/2]

blocks-0.5

Magnify[Column[blocks, Spacings -> -.14], 3]

blocks-3

I actually got good results with magnifications up to 5.

However, when I tried a magnification of 6.4, I ran into the same problem that the question demonstrates. I could find no setting for Spacings that removed the gap between the blocks. Instead, the rendering engine start to eat up the lower block and I made the spacing more negative. The window capture below shows this.

window-1

Magnify isn't the only source of trouble. Look what happen when I shrink the notebook window.

window-2

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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