Documentation Center

  • Trials
  • Product Updates

Optimizing Graphics Performance

Introduction

This section discusses techniques that can help increase the speed with which MATLAB® creates graphs. These techniques apply to cases when you are creating many graphs of similar data and can improve rendering speed by preventing MATLAB from performing unnecessary operation.

Whether a given technique improves performance depends on the particular application. The profile function can help you determine where your code is consuming the most time.

General Performance Guidelines

The following list provides some general guidelines for optimizing performance:

  • Set automatic-mode properties to manual whenever possible to prevent MATLAB from performing unnecessary operations.

  • Modify existing objects instead of creating new ones.

  • Use low-level core objects when creating objects repeatedly.

  • Do not recreate legends or other annotations in a program loop; add these after you finish modifying the graph.

  • Set the text Interpreter property to none if you are not using TeX characters.

  • Try various renderers and erase modes. MATLAB might not have auto-selected the fastest renderer for your application.

The remainder of this section provides more details on these and other techniques.

Disabling Automatic Modes

Graphics objects have properties that control many aspects of their behavior and appearance. The axes object in particular has many mode properties that enable it to respond to changes in the data represented in a graph.

For example, when you plot data, the axes determines appropriate axis limits, tick-mark placement, and labeling. Any changes you make to the plotted data (adding another line graph, for example) cause the axes to recompute the axis limits and to determine what values to use for the tick marks.

Fixing Axis Limits

The process of recalculating axis limits and the locations of the tick marks along each axis contributes to the time it takes to create a graph. If you are plotting data into the same axes repeatedly, you can improve performance by manually setting some or all of the axis limits, which, in turn, disables axis scaling and tick picking.

For example, suppose you are plotting time-series graphs in which you always view a 200-second time interval along the x-axis and your data ranges from –1 to 1. The following statement creates an axes with these limits and, in the process, sets the limit-picking mode to manual. Thereafter, MATLAB does not change the limits or recalculate tick mark locations (as long as you do not call a high-level plotting function like plot):

axes('XLim',[0 200],'YLim',[-1 1])

Setting All Modes to Manual

To maximize the efficiency with which MATLAB can update your graphs, disable all automatic operation so that MATLAB does not need to spend time determining if it is even necessary to recalculate a property value. The following steps illustrate this technique:

  1. Create a figure and select the renderer you want to use. Line graphs should use painters to take advantage of its line thinning algorithm:

    figure('Renderer','painters')
    

    Setting a property automatically sets its associated mode property to manual.

  2. Create an axes explicitly and set all properties (such as the axis limits) for which you can predetermine the appropriate value.

  3. Set all other mode property values to manual (see the next table).

  4. If you are creating line graphs using the painters renderer, set the axes DrawMode property to fast.

  5. If you cannot determine the appropriate value for all mode properties, create your first graph and then use the set command to set mode properties to manual. See Changing Graph Data Rapidly for an example.

The following table lists the axes mode properties and provides an explanation of what the mode controls.

Mode Property

What It Controls

ALimMode

Transparency limits mode

CameraPositionMode

Positioning of the viewpoint

CameraTargetMode

Positioning of the camera target in the axes

CameraUpVectorMode

The direction of "up" in 2-D and 3-D views

CameraViewAngleMode

The size of the projected scene and stretch-to-fit behavior

CLimMode

Mapping of data values to colors

DataAspectRatioMode

Relative scaling of data units along x-, y-, and z-axes and stretch-to-fit behavior

DrawMode

Controls the way MATLAB renders graphics objects (use with line graphs)

PlotBoxAspectRatioMode

Relative scaling of plot box along x-, y-, and z-axes and stretch-to-fit behavior

TickDirMode

Direction of axis tick marks (in for 2-D, out for 3-D)

XLimMode

YLimMode

ZLimMode

Limits of the respective x-, y-, and z-axes

XTickMode

YTickMode

ZTickMode

Tick mark spacing along the respective x-, y-, and z-axes

XTickLabelMode

YTickLabelMode

ZTickLabelMode

Tick mark labels along the respective x-, y-, and z-axes

Changing Graph Data Rapidly

MATLAB plotting functions perform a wide variety of operations in the process of creating a graph to make plotting easier. For example, the plot function clears the current axes before drawing new lines, selects a line color or a marker type, searches for user-defined default values, and so on.

Low-Level Functions for Speed

The features that make plotting functions easy to use also consume computer resources. If you want to maximize graphing performance, use low-level functions and disable certain automatic features.

Low-level graphics functions (e.g., line vs. plot, surface vs. surf) perform fewer operation and therefore can be faster when you are creating many graphics objects. See High-Level Versus Low-Level Functions for more information on how these functions differ.

Avoid Creating Graphics Objects

Each graphics object requires a certain amount of the computer's resources to create and store information, such as the value of all the object's properties. It is more efficient to use fewer graphics objects, if possible.

For example, add NaNs to vertex data (which causes that vertex to not be rendered) to create line segments that look like separate lines. Place the NaNs at identical locations in each vector of data:

x = [rand(5,1);nan;rand(4,1);nan;rand(6,1)];
y = [rand(5,1);nan;rand(4,1);nan;rand(6,1)];
line(x,y);

Update the Object's Data

To view different data on what is basically the same graph, it is more efficient to update the data of the existing objects (lines, text, etc.) rather than recreating the entire graph.

For example, suppose you want to visualize the effect on your data of varying certain parameters. Follow these steps:

  1. Set the limits of any axis that can be determined in advance, use max and min to determine the range of your data.

  2. Recalculate the data using the new parameters.

  3. Use the new data to update the data properties of the lines, text, etc. objects used in the graph.

  4. Call drawnow to flush the event queue and update the figure (and all child objects in the figure).

The following example illustrates the use of these techniques in a GUI that uses sliders to vary two parameters in a mathematical expression, which is then plotted.

Specify Axes with Plotting Function for Better Performance

Most plotting functions accept an axes handle as an argument. This handle determines the target axes for the plot. Specifying the parent axes as an argument is often faster than using the axes function to make a particular axes the current axes. For example, suppose you want six subplot in one figure and want to plot to each one in sequence:

x = 0:.05:2*pi;
for k=1:6 
    h(k) = subplot(6,1,k); 
end 
for k=1:6
    y = sin(x*100*rand)*rand;
    plot(h(k),x,y) % Specify target axes in plotting function
    % Avoid using axes(h(k); plot(x,y) in a loop
end

Keeping Track of the Target Figure and Axes

You can explicitly set the figure CurrentAxes property to avoid specifying the axes handle with a number of functions that can operate on the current axes by default. For example, in the following code, both stem and axis operate on the current axes if you do not specify an axes as an argument:

fHandle = figure;
for k=1:6 
    h(k) = subplot(6,1,k); 
end 
x = 0:.1:2*pi;
for k=1:6
    y = sin(x*100*rand)*rand;
    % Explicitly set current axes as a figure property
    set(fHandle,'CurrentAxes',h(k)); stem(x,y);
    axis([0 6.28 -1 1])
end

Both techniques give better performance than calling the axes function to control the current axes.

Performance of Bit-Mapped Images

Images can be defined with lower precision (than double) values to reduce the total amount of data required. MATLAB performs many operations on nondouble data types so you can use smaller image data without converting the data to type double. See 8-Bit and 16-Bit Images for more information.

Direct Color Mapping

Where possible, use indexed images, because indexed images can apply direct mapping of pixel values to colormap values (CDataMapping set to direct). With direct mapping, MATLAB does not need to scale the data and then map it to values in the colormap.

See the CDataMapping image property for more information.

Use Truecolor for Smaller Images

The use of truecolor (red, green, and blue values) eliminates the need for color mapping. However, with very large images, the data can be quite large and slow performance.

Direct Mapping of Transparency Values

If you are using an alphamap of transparency values, prescale the alpha data so you can use the most efficient alpha data mapping (AlphaDataMapping set to none).

See the AlphaDataMapping image property for more information.

Performance of Patch Objects

Improve the speed with which MATLAB renders patch objects using the following techniques.

Define Patch Faces as Triangles

If you are using patch objects that have many vertices per patch face, modify your data so that each face has only three vertices, but still looks like your original object. This eliminates the tessellation step from the rendering process.

Use Data Thinning

It is sometimes possible (or even desirable) to reduce the number of vertices in a patch and still produce the desired results.

See the reducepatch and reducevolume functions for more information.

Direct Color Mapping

Where possible, use direct color mapping for coloring patches. (CDataMapping set to direct). With direct mapping, MATLAB does not need to scale the data and then map it to values in the colormap.

See the CDataMapping patch property for more information.

Use Truecolor for Smaller Patches

The use of truecolor (red, green, and blue values) eliminates the need for color mapping. However, with very large patches the data can be quite large and slow performance.

Direct Mapping of Transparency Values

If you are using an alphamap of transparency values, prescale the alpha data so you can use the most efficient alpha data mapping (AlphaDataMapping set to none).

See the AlphaDataMapping patch property for more information.

Performance of Surface Objects

Improve the speed with which MATLAB renders surface objects using the following techniques.

Direct Color Mapping

Where possible, use direct color mapping for coloring surfaces (CDataMapping set to direct). With direct mapping, MATLAB does not need to scale the data and then map it to values in the colormap.

See the CDataMapping surface property for more information.

Use Truecolor for Smaller Surfaces

The use of truecolor (red, green, and blue values) eliminates the need for color mapping. However, with very large surfaces, the data can be quite large and slow performance.

Mapping of Transparency Values

If you are using an alphamap of transparency values, prescale the alpha data so you can use the most efficient alpha data mapping (AlphaDataMapping set to none)

See the AlphaDataMapping surface property for more information.

Use Texture-Mapped Face Color

If you are using surface objects in an animation or want to be able to pan and rotate them quickly, you can achieve better rendering performance with large surfaces by setting EdgeColor to none and FaceColor to texture.

This technique is particularly useful if you want a high resolution surface without creating an objects whose data is large and therefore, very slow to transform. For example:

h1 = surf(peaks(1000));
shading interp
cd1 = get(h1,'CData');
surf(peaks(24),'FaceColor','Texture','EdgeColor','none',...
	'CData',cd1)
Was this topic helpful?