5
$\begingroup$

I am experiencing a problem that a left margin is added to my Graphics if I include an arrow that is rotated by 3Pi/4 and shifted by -1. And only for those values. Here is a demo of my problem:

Grid[Table[
  Graphics[{Thick, Arrowheads[0.05], {
     Translate[Rotate[Arrow[{{-1/2, 0}, {1/2, 0}}], phi], shift],
     Line[{{-1.7, 0}, {1.7, 0}}],
     Line[{{-1, -1.7}, {-1, 1.7}}]
     }}, AspectRatio -> 1, ImageMargins -> 0],
  {phi, 0, 3 Pi/4, Pi/4},
  {shift, {{1, 0}, {0, 1}, {-1, 0}}}
  ],
 Frame -> All]

The lines are just to ensure that the bounding box of the resulting graphics remains unchanged. Here is the output:

enter image description here

All graphics are centered and have a symmetric margin, apart from the bottom-left one. Probably related is that arrows by themselves apparently do not rotate around the center of their bounding box:

Grid[{Table[Graphics[{Thick, Arrowheads[0.1], {
      Rotate[Arrow[{{-1/2, 0}, {1/2, 0}}], phi]
      }}, AspectRatio -> 1],
   {phi, 0, 3 Pi/4, Pi/4}]
  },
 Frame -> All]

enter image description here

Note that the arrows in the first image did rotate correctly around their bounding box. For instance, in the bottom-right cell, the center of the rotated and shifted arrow is exactly at the intersection of the two lines, i.e., at (-1,0) as expected from the translation. Still, all these problems disappear if I explicitly specify the rotation center via

Rotate[Arrow[{{-1/2, 0}, {1/2, 0}}], phi, {0, 0}]

Is this a bug?

$\endgroup$
1
  • 1
    $\begingroup$ You also have a top margin added to the graphic in grid cell {3, 2}. $\endgroup$
    – m_goldberg
    Commented Feb 21, 2017 at 7:32

2 Answers 2

4
$\begingroup$

1) The rotations and translations are being made correctly in all the grid cells. It is the plot range that is shifting. I don' know why it only affect two of the cells. It can be fixed by explicitly giving the plot range as well by explicitly giving the rotation center.

Grid[
  Table[
    Graphics[
      {Thick, Orange,
       Line[{{-1.7, 0}, {1.7, 0}}], Line[{{-1, -1.7}, {-1, 1.7}}],
       Black, Arrowheads[0.05], 
       {Translate[Rotate[Arrow[{{-1/2, 0}, {1/2, 0}}], phi], shift]}},
      PlotRange -> {{-1.7, 1.7}, {-1.7, 1.7}},
      ImageMargins -> 0],
    {phi, 0, 3 Pi/4, Pi/4},
    {shift, {{1, 0}, {0, 1}, {-1, 0}}}],
  Frame -> All]

grid1

2) If you think the grid you made was bad, it is good that you didn't try GraphicsGrid which really makes a mess.

GraphicsGrid[
  Table[
    Graphics[
      {Thick, Orange,
       Line[{{-1.7, 0}, {1.7, 0}}], Line[{{-1, -1.7}, {-1, 1.7}}],
       Black, Arrowheads[0.05], 
       {Translate[Rotate[Arrow[{{-1/2, 0}, {1/2, 0}}], phi], shift]}},
      ImageSize -> 167,
      ImageMargins -> 0],
    {phi, 0, 3 Pi/4, Pi/4},
    {shift, {{1, 0}, {0, 1}, {-1, 0}}}],
  Frame -> All]

grid2

This too is fixed by explicitly giving the rotation center or the plot range.

Update

I reported both problems, both the original one concerning Grid and the one concerning GraphicsGrid. I have received a reply. I quote the relevant sections:

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. Meanwhile, you can use the workarounds provided inside the sent notebook and ones on the [Mathematica.SE] thread.

Regarding the slight variations in the image size of the graphics when used along the function Grid, please consider the following.

Mathematica code set the unspecified graphics options such as PlotRange, PlotRangePadding, and ImagePadding, to automatic. Mathematica underlying algorithm updates these option values after each transformation. So the image size might (and in this case will) change as the arrow rotates.

The slight difference in the image size of the graphics causes the irregularity that you observed when using Grid without defining the rotation center or PlotRange explicitly.

I concluded from this that WRI considers the GraphicsGrid behavior a bug, but not the Grid behavior.

$\endgroup$
3
  • $\begingroup$ Thanks, this post identifies the core of the problem: PlotRange. Would you consider the behavior a bug? $\endgroup$
    – Felix
    Commented Feb 21, 2017 at 20:19
  • $\begingroup$ @Felix. I do think it's a bug. I plan to file a problem report on this topic with WRI tech support. $\endgroup$
    – m_goldberg
    Commented Feb 22, 2017 at 5:19
  • $\begingroup$ If WRI doesn't consider this a bug, I think explicitly specifying the plot range should be considered the solution. Thanks for communicating the issue. $\endgroup$
    – Felix
    Commented Feb 28, 2017 at 0:30
6
$\begingroup$

I never got warm with transforming graphics instead of just doing the calculation on the coordinates themselves. This is a clear example why it can be unnecessarily hard to use this.

If you instead use your exact same approach but transform the coordinates this issue disappears.

Grid[Table[Graphics[{Thick, Arrowheads[0.05], {Arrow[
      TranslationTransform[shift][
       RotationTransform[phi][{{-1/2, 0}, {1/2, 0}}]]], 
     Line[{{-1.7, 0}, {1.7, 0}}], Line[{{-1, -1.7}, {-1, 1.7}}]}}, 
   AspectRatio -> 1, ImageMargins -> 0], {phi, 0, 3 Pi/4, 
   Pi/4}, {shift, {{1, 0}, {0, 1}, {-1, 0}}}], Frame -> All]

Mathematica graphics

Grid[{Table[
   Graphics[{Thick, 
     Arrowheads[
      0.1], {Arrow[RotationTransform[phi][{{-1/2, 0}, {1/2, 0}}]]}}, 
    AspectRatio -> 1], {phi, 0, 3 Pi/4, Pi/4}]}, Frame -> All]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ +1 for another option that actually works. However, my approach should also work, shouldn't it? $\endgroup$
    – Felix
    Commented Feb 21, 2017 at 3:57
  • 1
    $\begingroup$ @Felix You could always fix the PlotRange but it is not entirely clear why this happens with Arrow. If you replace your Arrow with small Rectangle then the issue doesn't appear. Btw, you can see that your arrows are rotated around the center. The plot range is just off in your example. $\endgroup$
    – halirutan
    Commented Feb 21, 2017 at 4:02

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.