7
$\begingroup$

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:

Enter image description here

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?

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

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

enter image description here

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.

$\endgroup$
4
$\begingroup$

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]

enter image description here

Use t = .2 to get

enter image description here

$\endgroup$
3
$\begingroup$

You can change the AxisOrgin.

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

enter image description here

Or

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

enter image description here

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

enter image description here

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

enter image description here

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – ruffle
    Nov 17 '20 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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