Mathematica/2D Graphics/Tips and Tricks

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Like any software, Mathematica has certain features, bugs, quirks and defaults that can cause problems for those unaware of the solutions, and this is especially noticeable in graphical output.

Removing white borders[edit]

By default, Mathematica includes a white border around graphics. Normally in a notebook environment, this is not problem, as the background of the editor is white, and you can't see it. However, when you export the image, this can be annoying, especially as Mathematica includes the border in the image size, so the actual image is different size once cropped.

First, we will make a 2D graphics object graphic consisting of a blue square and two red circles to demonstrate:

graphic = Graphics[
    {Blue, Rectangle[{-1.5, -1.5}, {4, 4}]},
    {Red, Disk[{0, 0}, 1]},
    {Red, Disk[{2, 2}, 1.5]}

If we use Show[] to display this graphic, or we export it, a white border will be included in the image size. We can use the following option command to override this default:-> False}

Below is this graphic shown with (left) and without (right) the default border. A light grey border has been added in the markup for this page to highlight it, but this is not present in the actual images. You can see that the blue rectangle of the left image is not 250 pixels wide, but slightly smaller, although both images are exactly 250 pixels wide.

With default white border   Without default white border
Mathematica Tutorial - White border removal 1.png
Mathematica Tutorial - White border removal 2.png
 ImageSize -> 250
 ImageSize -> 250,
 Method ->{"ShrinkWrap"->True}