Mathematica grid line overflow

Mathematica During my previous usage of Mathematica commands to create a low-poly image with triangle patterns, I found an issue where the generated image consistently had white borders. But attempting to precisely control the image size with the crop command is useless, I have to process the image using highlight and binarization techniques. However, the issue of white borders remained unresolved. So, while I plot function graphs in Mathematica and set up grid lines, I found that the edges of the grid would often exceed the intended boundary. This resulted in the grid appearing as if it were laid over the function graph. So, I dedicated some time to search a solution and put my findings here.

Default Grid Lines

Upon opening a Mathematica notebook and plotting a colorful sine function curve with the command,

1
Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic, GridLinesStyle -> Directive[RGBColor[0.5, 0.8, 0.2]], ColorFunction -> "BlueGreenYellow", ImageSize -> 600]

The following image is obtained:

Mathematica grid line overflow

Examining the y-axis of the image shows that the grid extends beyond the intended boundary. This issue primarily arises from using the GridLines command where. Although the grid is confined within the PlotRange, Mathematica inadvertently adds extra padding around the image, causing the grid to fill beyond the coordinate lines.

Solution One

The simplest solution is to use the PlotRangePadding command to restrict Mathematica automatic extension of the padding area. According to the Mathematica help files, an additional 2% padding is added in each direction during plotting. The following command effectively limits this padding:

1
Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic, GridLinesStyle -> Directive[RGBColor[0.5, 0.8, 0.2]], ColorFunction -> "BlueGreenYellow", ImageSize -> 600, PlotRangePadding -> 0]

By constraining the padding, Mathematica now only shows the figure within the specified PlotRange,

Mathematica grid line overflow

This approach is also used for generating Graphics images. Without PlotRangePadding, images tend to have white borders:

Mathematica grid line overflow

However, these images are unsuitable for creating masks without adding PlotRangePadding->0. This produces the following result:

Mathematica grid line overflow

This adjustment significantly simplifies the process of mask application. Of course, if perfectionism isn’t a concern, adjusting the coordinate range slightly to accommodate the automatic 2% padding can also be a useful workaround:

Mathematica grid line overflow

Solution Two

Alternatively, using a Frame to contain the image can effectively prevent any overflow beyond its boundaries. We can plot with the following command,

1
Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic, GridLinesStyle -> Directive[RGBColor[0.5, 0.8, 0.2]], ColorFunction -> "BlueGreenYellow", Frame -> True, ImageSize -> 600]

Generate the following figure,

Mathematica grid line overflow

A closer inspection shows that the automatic padding issue persists within the Frame interior. This allows the image to appear more complete. This can also be addressed by setting PlotRangePadding->0 for a fuller image display,

Mathematica grid line overflow

Moreover, adjusting the Frame visibility can mimic the appearance of a standard coordinate axis,

1
Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic, GridLinesStyle -> Directive[RGBColor[0.5, 0.8, 0.2]], ColorFunction -> "BlueGreenYellow", Frame -> {{True, False}, {True, False}}, FrameTicks -> {{Automatic, None}, {Automatic, None}}, PlotRangePadding -> 0, ImageSize -> 600]

Resulting in the following graphic:

Mathematica grid line overflow

Adding AxesStyle -> Opacity[0] can completely hide the mid line in the figure,

This creates a visually appealing result:

Mathematica grid line overflow

Conclusion

It took me considerable effort to resolve this issue, primarily because the Graphics documentation did not explain how to eliminate white borders. Consequently, the idea of creating graphical masks remained unfulfilled until today’s discovery with Plot that grid lines exceeded their boundaries. This indicated that PlotRangePadding could provide a solution, and indeed, it proved effective.

The full script can be downloaded here: DOWNLOAD