## Documentation |

On this page… |
---|

Graphics objects are the basic elements used to display graphs. These objects are organized into a hierarchy, as shown by the following diagram.

When you call a plotting function, MATLAB^{®} creates the graph
using various graphics objects, such as a figure window, axes, lines,
text, and so on. Each object has a fixed set of properties, which
you can use to control the behavior and appearance of the graph.

For example, the following statement creates a bar graph from the data in the variable y and sets properties that how the bars look:

y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y,'FaceColor','green','EdgeColor','black','LineWidth',1.5)

When you call a function to create a graph, MATLAB creates
a hierarchy of graphics objects. For example, calling the `plot` function creates the following graphics
objects:

Figure — Window that contains axes, toolbars, menus, and so on.

Axes — Coordinate system that contains the object representing the data

Line — Lines that represent the value of data passed to the

`plot`function.Text — Labels for axes tick marks and optional titles and annotations.

Different types of graphs use different objects to represent data. Because there are many kinds of graphs, there are many types of data objects. Some are general purpose, such as lines and rectangles and some are highly specialized, such as errorbars, colorbars, and legends.

Plotting functions can return the objects used to create the
graph. For example, the following statements create a graph and return
the line object created by the `plot` function:

x = 1:10; y = x.^3; h = plot(x,y);

Use `h` to set the properties of the line object.
For example, set its `Color` property.

`h.Color = 'red';`

You can also specify the line properties when you call the plotting function.

h = plot(x,y,'Color','red');

You can query the line properties to see the current value:

h.LineWidth

ans = 0.5000

To view the properties of an object, type:

get(h)

MATLAB returns a list of the object's properties with the current values.

To view an object's properties with information about possible values, type:

set(h)

You can set multiple properties at once using the `set` function.

To set the same property to the same value on multiple objects,
use the `set` function.

For example, the following statements plot a 5-by-5 matrix (creating
five line objects, one per column), and then set the `Marker` property
to square and the `MarkerFaceColor` property to green.

y = magic(5); h = plot(y); set(h,'Marker','s','MarkerFaceColor','g')

In this case, `h` is a vector containing five
handles, one for each of the five lines in the graph. The `set` statement
sets the `Marker` and `MarkerFaceColor` properties
of all lines to the same value.

To set a property value on one object, index into the handle array:

h(1).LineWidth = 2;

If you want to set the properties of each line to a different
value, you can use cell arrays to store all the data and pass it to
the `set` command. For example,
create a plot and save the line handles:

figure y = magic(5); h = plot(y);

Suppose you want to add different markers to each lines and color the marker's face color the same color as the line. You need to define two cell arrays—one containing the property names and the other containing the desired values of the properties.

The `prop_name` cell array contains two elements:

prop_name(1) = {'Marker'}; prop_name(2) = {'MarkerFaceColor'};

The `prop_values` cell array contains 10 values:
five values for the `Marker` property and five values
for the `MarkerFaceColor` property. Notice that `prop_values` is
a two-dimensional cell array. The first dimension indicates which
handle in `h` the values apply to and the second
dimension indicates which property the value is assigned to:

prop_values(1,1) = {'s'}; prop_values(1,2) = {h(1).Color}; prop_values(2,1) = {'d'}; prop_values(2,2) = {h(2).Color}; prop_values(3,1) = {'o'}; prop_values(3,2) = {h(3).Color}; prop_values(4,1) = {'p'}; prop_values(4,2) = {h(4).Color}; prop_values(5,1) = {'h'}; prop_values(5,2) = {h(5).Color};

The `MarkerFaceColor` is always assigned
the value of the corresponding line's color (obtained by getting the
line `Color` property).

After defining the cell arrays, call `set` to
specify the new property values:

set(h,prop_name,prop_values)

This table lists functions commonly used when working with objects.

Function | Purpose |
---|---|

Find all children of specified objects. | |

Find ancestor of graphics object. | |

Copy graphics object. | |

Delete an object. | |

Find all graphics objects (including hidden handles). | |

Find the handles of objects having specified property values. | |

Return the handle of the current axes. | |

Return the handle of the current figure. | |

Return the handle of the current object. | |

Query the values of an object's properties. | |

True if the value is a valid object handle. | |

Set the values of an object's properties. |

You can define specialized plotting functions to simplify the creation of customized graphs. By defining a function, you can pass arguments like MATLAB plotting functions.

The following example shows a MATLAB function that evaluates
a mathematical expression over the range specified in the input argument `x`,
and then plots the results. A second call to the `plot` function plots the `mean` value of the results as a red line.

The function modifies the y-axis ticks based on the values calculated. Axis labels and a title finish the custom graph.

function plotFunc(x) y = 1.5*cos(x) + 6*exp(-.1*x) + exp(.07*x).*sin(3*x); ym = mean(y); hfig = figure('Name','Function and Mean'); hax = axes('Parent',hfig); plot(hax,x,y) hold on plot(hax,[min(x) max(x)],[ym ym],'Color','red') hold off ylab = hax.YTick; new_ylab = sort([ylab, ym]); hax.YTick = new_ylab; title ('y = 1.5cos(x) + 6e^{-0.1x} + e^{0.07x}sin(3x)') xlabel('X Axis') ylabel('Y Axis') end

Define a value for the input argument and call the function.

x = -10:.005:40; plotFunc(x)

The `findobj` function
enables you to obtain the handles of graphics objects by searching
for objects with particular property values. With `findobj` you
can specify the values of any combination of properties, which makes
it easy to pick one object out of many. `findobj` also
recognizes regular expressions.

Because all objects have a `Type` property
that identifies the type of object, you can find the handles of all
occurrences of a particular type of object. For example,

h = findobj('Type','patch');

finds the handles of all patch objects.

You can specify multiple properties to narrow the search. For example,

plot(rand(5),'r:') h = findobj('Type','line','Color','r','LineStyle',':');

finds the handles of all red dotted lines.

h = 5x1 Line array: Line Line Line Line Line

You can specify the starting point in the object hierarchy by passing the handle of the starting figure or axes as the first argument. For example,

h = findobj(gca,'Type','text','String','\pi/2');

finds the string π/2 only within the current axes.

Was this topic helpful?