2.9.4 Coordinate Systems for Two-Dimensional

2. Principles of Mathematica
476
2.9.4 Coordinate Systems for Two-Dimensional Graphics
When you set up a graphics object in Mathematica, you give coordinates for the various graphical elements that appear. When Mathematica renders the graphics object, it has to translate the original coordinates you gave into “display coordinates” which specify where each element should be placed in the
final display area.
Sometimes, you may find it convenient to specify the display coordinates for a graphical element
directly. You can do this by using “scaled coordinates” 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 lowerleft corner of the display area.
{x, y}
Scaled {sx, sy}]
original coordinates
scaled coordinates
Coordinate systems for two-dimensional graphics.
The rectangle is drawn at a fixed
position relative to the display area,
independent of the original coordinates
used for the plot.
In 1]:= Plot Tan x], {x, 0, 2Pi},
Prolog ->
Rectangle Scaled {0.7, 0.7}], Scaled {1, 1}]]]
40
20
1
2
3
4
5
6
-20
-40
When you use {x, y} or Scaled {sx, sy}], you are specifying 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 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.
Note that you can use Scaled with either one or two arguments to specify radii in Disk and Circle
graphics elements.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.9 The Structure of Graphics and Sound
477
PlotRange -> {{xmin, xmax}, {ymin, ymax}}
the range of original coordinates to include in the plot
PlotRegion -> {{sxmin, sxmax}, {symin, symax}}
the region of the display specified in scaled coordinates
which the plot fills
Options which determine translation from original to display coordinates.
When Mathematica 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 PlotRange specifies the range of original coordinates to include. As discussed on
page 165, the default setting is PlotRange -> Automatic, which makes Mathematica try to choose
a range which includes all “interesting” parts of a plot, while dropping “outliers”. By setting
PlotRange -> All, you can tell Mathematica 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.
In 2]:= obj = Polygon
In this case, the polygonal object fills
almost the whole display area.
In 3]:= Show Graphics obj]]
With the default
PlotRange -> Automatic, the outlying
point is not included, but does affect the
range of coordinates chosen.
In 4]:= Show Graphics {obj, Point {20, 20}]}] ]
Table {Sin n Pi/10], Cos n Pi/10]} + 0.05 (-1)^n,
{n, 20}]] Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2. Principles of Mathematica
478
With PlotRange -> All, the outlying
point is included, and the coordinate
system is correspondingly modified.
In 5]:= Show %, PlotRange -> All]
The option PlotRange allows you to specify a rectangular region in the original coordinate system,
and to drop any graphical elements that lie outside this region. In order to render the remaining elements, however, Mathematica then has to determine how to position this rectangular region with respect to the final display area.
The option PlotRegion allows you to specify where the corners of the rectangular region lie within
the final display area. The positions of the corners are specified in scaled coordinates, which are defined to run from 0 to 1 across the display area. The default is PlotRegion -> {{0, 1}, {0, 1}},
which specifies that the rectangular region should fill the whole display area.
By specifying PlotRegion, you can
effectively add “margins” around your
plot.
In 6]:= Plot ArcTan x], {x, 0, 10},
PlotRegion -> {{0.2, 0.8}, {0.3, 0.7}}]
1.4
1.2
1
0.8
0.6
0.4
0.2
2
AspectRatio -> r
AspectRatio -> Automatic
4
6
8 10
make the ratio of height to width for the display area equal
to r
determine the shape of the display area from the original
coordinate system
Specifying the shape of the display area.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.9 The Structure of Graphics and Sound
479
What we have discussed so far is how Mathematica 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 Mathematica display area must fit. How it fits into this region is determined by its “shape” or aspect ratio. In
general, the option AspectRatio specifies the ratio of height to width for the final display area.
It is important to note that the setting of 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
AspectRatio does is to change the shape of this display area.
This generates a graphic object
corresponding to a hexagon.
In 7]:= hex = Graphics Polygon
This renders the hexagon in a display
area whose height is three times its
width.
In 8]:= Show hex, AspectRatio -> 3]
Table {Sin n Pi/3], Cos n Pi/3]}, {n, 6}] ]] For two-dimensional graphics, AspectRatio is set by default to the fixed value of 1/GoldenRatio.
Sometimes, however, you may want to determine the aspect ratio for a plot from the original coordinate system used in the plot. Typically what you want is for one unit in the x direction in the original
coordinate system to correspond 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”. You can make this happen by setting the option AspectRatio -> Automatic.
With AspectRatio -> 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”.
In 9]:= Show hex, AspectRatio -> Automatic]
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
480
2. Principles of Mathematica
Using scaled coordinates, you can specify the sizes of graphical elements as fractions of the size of
the display area. You cannot, however, tell Mathematica 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 Mathematica. Nevertheless, graphics directives such as AbsoluteThickness discussed on page 471 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.)
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.