Coordinate Systems for Two-Dimensional Graphics

When you set up a graphics object in the Wolfram Language, you give coordinates for the various graphical elements that appear. When the Wolfram Language renders the graphics object, it has to translate the original coordinates you gave into "display coordinates" that specify where each element should be placed in the final display area.

Option that determines translation from original to display coordinates.

When the Wolfram Language renders a graphics object, one of the first things it has to do is to work out what range of original x and y coordinates it should actually display. Any graphical elements that are outside this range will be clipped, and not shown.

The option (RefLink[PlotRange, paclet:ref/PlotRange]) specifies the range of original coordinates to include. As discussed in "Options for Graphics", the default setting is (RefLink[PlotRange, paclet:ref/PlotRange])->(RefLink[Automatic, paclet:ref/Automatic]), which makes the Wolfram Language try to choose a range which includes all "interesting" parts of a plot, while dropping "outliers". By setting (RefLink[PlotRange, paclet:ref/PlotRange])->(RefLink[All, paclet:ref/All]), you can tell the Wolfram Language to include everything. You can also give explicit ranges of coordinates to include.

This sets up a polygonal object whose corners have coordinates between roughly ±1:

obj=Polygon[Table[{Sin[n Pi/10],Cos[n Pi/10]}+0.05 (-1)^n,{n,20}]];

In this case, the polygonal object fills almost the whole display area:

Graphics[obj]

Specifying an explicit (RefLink[PlotRange, paclet:ref/PlotRange]) allows you to zoom in on a section of a graphic:

Graphics[obj,PlotRange->{{0,1},All}]

Specifying the shape of the display area.

What we have discussed so far is how the Wolfram Language translates the original coordinates you specify into positions in the final display area. What remains to discuss, however, is what the final display area is like.

On most computer systems, there is a certain fixed region of screen or paper into which the Wolfram Language display area must fit. How it fits into this region is determined by its "shape" or aspect ratio. In general, the option (RefLink[AspectRatio, paclet:ref/AspectRatio]) specifies the ratio of height to width for the final display area.

It is important to note that the setting of (RefLink[AspectRatio, paclet:ref/AspectRatio]) does not affect the meaning of the scaled or display coordinates. These coordinates always run from 0 to 1 across the display area. What (RefLink[AspectRatio, paclet:ref/AspectRatio]) does is to change the shape of this display area.

For two-dimensional graphics, (RefLink[AspectRatio, paclet:ref/AspectRatio]) is set by default to (RefLink[Automatic, paclet:ref/Automatic]). This determines the aspect ratio from the original coordinate system used in the plot instead of setting it at a fixed value. One unit in the x direction in the original coordinate system corresponds to the same distance in the final display as one unit in the y direction. In this way, objects that you define in the original coordinate system are displayed with their "natural shape".

This generates a graphic object corresponding to a regular hexagon. With the default value of (RefLink[AspectRatio, paclet:ref/AspectRatio])->(RefLink[Automatic, paclet:ref/Automatic]), the aspect ratio of the final display area is determined from the original coordinate system, and the hexagon is shown with its "natural shape":

Graphics[Polygon[Table[{Sin[n Pi/3],Cos[n Pi/3]},{n,6}]]]

This renders the hexagon in a display area whose height is three times its width:

Show[%,AspectRatio->3]

Sometimes, you may find it convenient to specify the display coordinates for a graphical element directly. You can do this by using scaled coordinates (RefLink[Scaled, paclet:ref/Scaled])[{sx,sy}] rather than {x,y}. The scaled coordinates are defined to run from 0 to 1 in x and y, with the origin taken to be at the lower-left corner of the plot range.

Coordinate systems for two-dimensional graphics.

The display area is significantly larger than the plot range due to the frame label:

g=Graphics[{Green,Disk[]},PlotRange->2,Frame->True,FrameLabel->{Style["x",Large]}]

Using (RefLink[Scaled, paclet:ref/Scaled]) coordinates, the rectangle falls at the origin, which is at the center of the specified plot range:

Show[g,Prolog->{Rectangle[Scaled[{0.25,0.25}],Scaled[{0.75,0.75}]]}]

Using (RefLink[ImageScaled, paclet:ref/ImageScaled]) coordinates, the rectangle falls at exactly the center of the graphic, which does not coincide with the center of the plot range:

Show[g,Prolog->{Rectangle[ImageScaled[{0.25,0.25}],ImageScaled[{0.75,0.75}]]}]

When you use {x,y}, (RefLink[Scaled, paclet:ref/Scaled])[{sx,sy}], or (RefLink[ImageScaled, paclet:ref/ImageScaled])[{sx,sy}], you are specifying the position either completely in original coordinates, or completely in scaled coordinates. Sometimes, however, you may need to use a combination of these coordinate systems. For example, if you want to draw a line at a particular point whose length is a definite fraction of the width of the plot, you will have to use original coordinates to specify the basic position of the line, and scaled coordinates to specify its length.

You can use (RefLink[Scaled, paclet:ref/Scaled])[{dsx,dsy},{x,y}] to specify a position using a mixture of original and scaled coordinates. In this case, {x,y} gives a position in original coordinates, and {dsx,dsy} gives the offset from the position in scaled coordinates.

Some places where (RefLink[Scaled, paclet:ref/Scaled]) can be used with a single argument.

Both the radius of the circle and the size of the font are specified in (RefLink[Scaled, paclet:ref/Scaled]) values:

Graphics[{Circle[{0,0},Scaled[0.3]],FontSize->Scaled[0.2],Text["some text",{0,0}]}]

Positions specified as offsets.

Each line drawn here has an absolute length of six printer's points:

Graphics[Table[Line[{{x,x^2},Offset[{0,6},{x,x^2}]}],{x,10}],Frame->True]

You can also use (RefLink[Offset, paclet:ref/Offset]) inside (RefLink[Circle, paclet:ref/Circle]) with just one argument to create a circle with a certain absolute radius:

Graphics[Table[Circle[{x,x^2},Offset[{2,2}]],{x,10}],Frame->True]

In most kinds of graphics, you typically want the relative positions of different objects to adjust automatically when you change the coordinates or the overall size of your plot. But sometimes you may instead want the offset from one object to another to be constrained to remain fixed. This can be the case, for example, when you are making a collection of plots in which you want certain features to remain consistent, even though the different plots have different forms.

(RefLink[Offset, paclet:ref/Offset])[{adx,ady},position] allows you to specify the position of an object by giving an absolute offset from a position that is specified in original or scaled coordinates. The units for the offset are printer's points, equal to (1/72) of an inch.

When you give text in a plot, the size of the font that is used is also specified in printer's points. Therefore, a 10-point font, for example, has letters whose basic height is 10 printer's points. You can use (RefLink[Offset, paclet:ref/Offset]) to move text around in a plot, and to create plotting symbols or icons which match the size of the text.

Using scaled coordinates, you can specify the sizes of graphical elements as fractions of the size of the display area. You cannot, however, tell the Wolfram Language the actual physical size at which a particular graphical element should be rendered. Of course, this size ultimately depends on the details of your graphics output device, and cannot be determined for certain within the Wolfram Language. Nevertheless, graphics directives such as (RefLink[AbsoluteThickness, paclet:ref/AbsoluteThickness]) discussed in "Graphics Directives and Options" do allow you to indicate "absolute sizes" to use for particular graphical elements. The sizes you request in this way will be respected by most, but not all, output devices. (For example, if you optically project an image, it is neither possible nor desirable to maintain the same absolute size for a graphical element within it.)