# How can I put the edge of a rectangle inside it rather than outside it?

Problem: when I draw a rectangle and put a coloured edge around it, the displayed edge is centred along the nominal edge and if it follows the same course as one of the axes then it does not show up. For example:

Graphics[{EdgeForm[Red], FaceForm[], Rectangle[{0, 0}, {4, 3}]}, Axes -> True]


yields this:

The problem also arises when for example I want to show a grid of 1x1 squares with some edged in red and others in blue: along any given line-segment, only one colour shows.

How can I make the coloured "edge" of a rectangle sit inside the shape, touching the shape's boundary, but not running outside it?

• Adding the options Frame->True, Axes->False to Graphics[ ] should draw a frame around your rectangle(s) without the axes interfering with the plot.
– mjw
Commented Nov 24, 2020 at 22:11
• Actually, by default Axes->False for Graphics[ ]. Useful, though, for Plot[ ], Show[ ], etc.
– mjw
Commented Nov 24, 2020 at 22:12

The first way that comes to mind for me is to use Offset to "shrink" the rectangles by the width of the edges:

Graphics[Table[{
EdgeForm[{AbsoluteThickness[1], RandomChoice[{Red, Blue}]}], FaceForm[],
Rectangle[Offset[{1/2, 1/2}, {i, j}], Offset[{-1/2, -1/2}, {i + 1, j + 1}]]
}, {i, 7}, {j, 5}]]


Setting the thickness of the edges allows us to know how far in points we should shift the corners of the rectangles toward their centers.

We can mimic thick edges overlaying a white scaled rectangle over a colored rectangle:

t = .1;

Graphics[Table[
{EdgeForm[], If[EvenQ[i + j], Red, Blue], Rectangle[{i, j}, 1 + {i, j}],
FaceForm[White], Scale[Rectangle[{i, j}, 1 + {i, j}], 1 - 2 t]},
{i, 7}, {j, 5}],
ImageSize -> Large]


Use t = .2 to get

You can change the AxisOrgin.

Graphics[{EdgeForm[Red], FaceForm[], Rectangle[{0, 0}, {4, 3}]},
Axes -> True, AxesOrigin -> {-1, -1}]


Or

Graphics[{EdgeForm[Red], FaceForm[], Rectangle[{0, 0}, {4, 3}]},
Axes -> True, AxesOrigin -> {-0.1, -0.1}]


Or (in response to a comment from OP):

Show[{
Graphics[{EdgeForm[Red], FaceForm[], Rectangle[{0, 0}, {1, 1}]},
Axes -> True, AxesOrigin -> {-0.1, -0.1}],
Graphics[{EdgeForm[Blue], FaceForm[], Rectangle[{1, 1}, {2, 2}]},
Axes -> True, AxesOrigin -> {-0.1, -0.1}],
Graphics[{EdgeForm[Green], FaceForm[], Rectangle[{1, 0}, {2, 1}]},
Axes -> True, AxesOrigin -> {-0.1, -0.1}],
Graphics[{EdgeForm[Black], FaceForm[], Rectangle[{0, 1}, {1, 2}]},
Axes -> True, AxesOrigin -> {-0.1, -0.1}]
}]


But this doesn't get you separately outlined rectangles. Something like the following may get you closer:

Grid[{
{Graphics[{EdgeForm[Red], FaceForm[], Rectangle[{0, 0}, {1, 1}]}],
Graphics[{EdgeForm[Blue], FaceForm[],
Rectangle[{0, 0}, {1, 1}]}]},
{Graphics[{EdgeForm[Green], FaceForm[],
Rectangle[{0, 0}, {1, 1}]}],
Graphics[{EdgeForm[Black], FaceForm[],
Rectangle[{0, 0}, {1, 1}]}]}
}, Spacings -> {-0.5, -0.5}]


But this doesn't give you Axes.
One could construct Axes separately, if you really need them.

• Thanks for this, but that's not what I need. What I am actually doing is covering an area with 1x1 squares as described in my second paragraph, some of which should be edged in one colour and some in another. I used the example of a single rectangle with a corner at the origin to try and simplify the problem to its essentials.
– user59886
Commented Nov 17, 2020 at 19:18