4
$\begingroup$

When using ArrayPlot with both PixelConstrained->True and Mesh->True, the lower and right walls of the mesh both drop out:

ArrayPlot[Table[Mod[Ceiling[i/2] + Ceiling[j/2], 2], {i, 8}, {j, 8}], PixelConstrained -> 8, Mesh -> True]

missing walls

Setting Frame->True instead shortens the first row and last column:

ArrayPlot[Table[Mod[Ceiling[i/2] + Ceiling[j/2], 2], {i, 8}, {j, 8}], PixelConstrained -> 8, Mesh -> True, Frame -> True]

squashed cells

The only way I've found so far that solves this issue is to set the image size manually:

ArrayPlot[Table[Mod[Ceiling[i/2] + Ceiling[j/2], 2], {i, 8}, {j, 8}], Mesh -> True, ImageSize -> 67]

manual image size

However, this procedure is tedious, and has to be performed on every image separately. Plus, I don't think I can guarantee that all of the cells will have the same width and height. Is there any other way to fix this annoying imperfection? (I've had this problem since at least v7, and it's still present in v11.)

$\endgroup$
4
  • 1
    $\begingroup$ Maybe you could post the codes you used to make those plots. $\endgroup$
    – corey979
    Dec 16, 2016 at 1:21
  • $\begingroup$ @corey979 Done. $\endgroup$ Dec 16, 2016 at 19:31
  • $\begingroup$ I would consider this behavior a bug. It has annoyed me before too but I've never bothered to find a general solution. (Thanks for posting.) $\endgroup$
    – Mr.Wizard
    Dec 20, 2016 at 23:34
  • $\begingroup$ Related: (28246) $\endgroup$
    – Mr.Wizard
    Jan 31, 2017 at 21:50

3 Answers 3

4
$\begingroup$

Found a solution myself, although I had to get a bit creative:

ImageTake[ArrayPlot[ArrayPad[Table[Mod[Ceiling[i/2] + Ceiling[j/2], 2], {i, 8}, {j, 8}], 1], PixelConstrained -> 8, Mesh -> True], {9, -8}, {9, -8}]

crop

In a more general form:

ImageTake[ArrayPlot[ArrayPad[a, 1], PixelConstrained -> s, Mesh -> True, (*any other options*)], {s+1, -s}, {s+1, -s}]

This pads the array, in order to move the border issue to irrelevant cells, and then crops out those irrelevant cells from the final image, leaving only the relevant cells, yet preserving their borders.

$\endgroup$
2
$\begingroup$

Instead of using PixelConstrained -> 8, use the undocumented (but not going away) syntax ImageSize -> 1 -> 8. For your first example:

ArrayPlot[
    Table[Mod[Ceiling[i/2]+Ceiling[j/2],2],{i,8},{j,8}],
    ImageSize -> 1 -> 8,
    Mesh -> True
]

enter image description here

And your second example:

ArrayPlot[
    Table[Mod[Ceiling[i/2]+Ceiling[j/2],2],{i,8},{j,8}],
    ImageSize->1->8,
    Mesh->True,
    Frame->True,
    PlotRangePadding->0
]

enter image description here

where I threw in PlotRangePadding -> 0 to avoid a padding issue.

$\endgroup$
1
$\begingroup$

This seems to work in the example provided. Please test it and tell me if it fails.

fixPC = Show[#, PlotRangePadding -> 1, ImageSize -> 2 + PlotRange[#][[All, 2]]] &;

ArrayPlot[
  Array[Mod[⌈#/2⌉ + ⌈#2/2⌉, 2] &, {8, 8}]
  , Mesh -> True
  , PixelConstrained -> 12
] // fixPC

enter image description here

$\endgroup$
1
  • $\begingroup$ Also appears to work. $\endgroup$ Dec 22, 2016 at 0:32

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.