CS 211 Lesson 28
Graphics: Positions, Units, and Defaults
Quote:
"A set back is the opportunity to begin again more intelligently." Henry Ford
Lesson Objectives:
Understand the Position and Units properties of graphic objects.
Understand the coordinate system of axes objects.
Understand the positioning of text in a text object.
Understand how to set default values for graphic object properties.
Lesson:
I. MATLAB Concepts
A. The Position and Units graphic object properties
Many graphic objects (e.g., figure and axes objects) have a Position and Units property .
The Position property specifies an object's location (relative to its parent) and an object's size.
The Units property determines how the Position property is interpreted.
For figures and axes objects, the value of the Units property may be one of the following:
Units property possible values Description 'normalized' the Position values are a percentage distances in the range [0.0, 1.0] 'pixels' the Position values are interpreted as pixels 'inches' the Position values are interpreted as inches 'centimeters' the Position values are interpreted as centimeters 'points' the Position values are interpreted as font point sizes (there are 72 points in 1 inch) 'characters' the Position values are interpreted as distances as defined by characters from the default system font; one unit horizontally is the width of the letter x, and one unit vertically is the distance between the baselines of two lines of text.
- The default value for the Units property is 'pixels'.
- IMPORTANT: You should always set the Units property to 'normalized' and set position and size values in relative (percentage) terms. This is because normalized units are independent of screen resolution.
A figure's Position property identifies its location relative to the lower-left corner of the screen.
An axes' Position property identifies its location relative to the lower-left corner of its enclosing figure.
For figures and axes, the value of the Position property is a 4-element row vector in which:
element 1 identifies the position of the left edge of the drawable area (minimum x-axis value)
element 2 identifies the position of the bottom edge of the drawable area (minimum y-axis value)
element 3 identifies the width of the drawable area (excludes borders, scroll bars, etc.)
element 4 identifies the height of the drawable area (excludes title bar, menu bar, tool bar, etc.)
Consider the following examples:
% Set the position and size of a figure window in pixels - bad idea!
set(gcf(), 'Units', 'pixels');
set(gcf(), 'Position', [50 50 300 400]);
% Set the position and size of a figure window using percentages.
% Always use percentages because the values are independent of
% screen resolution.
set(gcf(), 'Units', 'normalized');
set(gcf(), 'Position', [0.25 0.25 0.5 0.5]);
The Units property of a figure window determines how the current position of the mouse cursor is reported. For example,
If the Units property is set to 'pixels', get(fig_handle, 'CurrentPoint') returns the position of the mouse as the number of pixels from the lower-left corner of the figure window.
If the Units property is set to 'normalized', get(fig_handle, 'CurrentPoint') returns the position of the mouse as a percentage of distance from the lower-left corner of the window as compared to the window's width and height.
B. Positioning objects within axes
An axes object represents a 2D or 3D coordinate system.
The Position and Units property of an axes object controls its location and size within a figure window -- NOT the units or size of the coordinate system it represents.
The XLim, YLim, and ZLim properties of an axes object define the limits of the axes' coordinate system.
The XLimMode, YLimMode, and ZLimMode properties of an axes control how the XLim, YLim, and ZLim properties are set.
The XLimMode, YLimMode, and ZLimMode properties can be set to either 'auto' or 'manual'.
If set to 'auto', MATLAB sets the XLim, YLim, and ZLim properties based on the minimum and maximum values of its children graphic objects.
If set to 'manual', the programmer must set the XLim, YLim, and ZLim properties to an appropriate range of values.
If you manually set one of the properties, XLim, YLim, and ZLim, e.g., set(axes_handle, 'XLim', [10 20]), then MATLAB sets its corresponding limits mode value to 'manual'.
If you query the location of the mouse cursor within an axes object, e.g.,
Location = get(Axes_handle, 'CurrentPoint'),
the location is always reported in coordinate system values. For example, the following code sets the limits of an axes coordinate system and then queries the location of the mouse cursor. The location vector that is returned will have an x-value in the range [10,20] and the y-value in the range [40,50].Axes_handle = gca();
set(Axes_handle, 'XLim', [10 20]);
set(Axes_handle, 'YLim', [40 50]);
waitforbuttonpress();
Location = get(Axes_handle, 'CurrentPoint')C. Positioning objects within themselves (e.g., text objects)
Some graphics objects have properties that position and orient their graphical representations relative to some reference point. One example of such an object is a text object.
A text object is a string of characters displayed within an axes.
Text objects are useful for labeling (i.e., annotating) other graphic objects within an axes.
Text objects are created using the text(X_position, Y_position, 'A message') function. The X_position and Y_position values are always specified in terms of the axes' coordinate system. For example, if you axes' coordinate system has x-values in the range [10,20] and has y-values in the range [40,50], then the following command will place the specified text exactly in the middle of the axes.
text(15, 45, 'Example');
The X_position and Y_position values specify a reference location (sometimes called an "anchor point") for the display of the text. The 'HorizontalAlignment' and 'VerticalAlignment' properties of the text object determine how the text is displayed relative to the anchor point. These properties are described in the table below.
text property Possible Values Meaning 'HorizontalAlignment' 'left' (default)
'center'
'right'anchor is at left end of the text string
anchor is at the center of the text string
anchor is at the right end of the text string'VerticalAlignment' 'top'
'cap'
'middle' (default)
'baseline'
'bottom'anchor is at the top of text's enclosing rectangle
anchor is at the top of capital letters
anchor is in the middle of the text's enclosing rectangle
anchor is at the baseline of the text's characters
anchor is at the bottom of the text's enclosing rectangle
- The positioning options in the chart above are illustrated in the diagrams below. The large dot represents the location of the anchor point.
Side Note: Other useful properties of a text object include: 'String', 'FontName', 'FontSize', 'FontWeight', and 'Color'.
D. Default properties
When a graphic object is created, all properties of the object are assigned an initial "default" value.
The initial default values are found by searching the handle graphics hierarchy starting with an object's parent. The first matching property value that is found is used for the initial default value.
The property names of default property values have 3 parts:
the string 'Default',
followed by an object type,
followed by the specific property name.
For example, the string 'DefaultLineLineWidth' is the default 'LineWidth' property for a line object.
Some examples:
If you want all new lines created in an axes object to have the color 'red', you could specify a default line color value in the axes object before any of the lines are created. For example:
set(gca(), 'DefaultLineColor', 'red');
If you want all new figures to have a specific color, say [0.5 0.3 0.7], then you could set a default color value for figures in the root object. For example: (Remember that the handle to the root object is always 0.)
set(0, 'DefaultFigureColor', [0.5 0.3 0.7]);
You can change object default values so that they are set to your specific needs every time you run MATLAB by adding appropriate set() commands to the file C:\Program Files\MATLAB\R2007a\toolbox\local\startup.m . (For most cadets this file does not exist. You would have to create the file and then add the set() commands.)
II. Good Programming Practices
Always specify the position and size of graphic objects using 'normalized' units.
III. Algorithms
(none for this lesson)
Lab Work: Lab 28
References: Chapman Textbook: section 9.8-9.11