Handle Graphics: Modifying Plots

Each part of a matlab plot has some set of properties that can be changed to change its appearance. You can create a "handle" for each part of the plot which basically lets you "grab" that part of the plot and change some of its properties. The two commands "get" and "set" together allow you to find out the current properties of everything in your plot, and change just about anything about them.

gca and gcf

You can always get the handle for two different parts of the plot: the axis, and the figure. The properties of the figure describe the general characteristics of your entire figure window. The axis has properties that describe the characteristics of your axes.

To list all properties of the figure, you can type get(gcf) (which stands for get current figure). To list all properties of the axis, you can type get(gca) (which stands for get current axis). The figure and the axis each have "children." What this means is there are objects in the plot which are a part of them. Each set of axes is in your figure window is a "child" of gcf. (If you have only one set of axes, the child of gcf is automatically gca.) Each thing plotted on a set of axes (gca) is a child of that gca. One of the properties that gca and gcf have is 'Children', which gives you a handle to its children so you can modify things like lines you've plotted, too.


The other useful handle you can get is gco. (get current object.) If you click on something in the matlab window (a particular piece of text, a particular line you've plotted, etc), and then type get(gco), you will get the properties of the thing you have just clicked on.

Syntax and Examples

To start with a pointer to the thing you want to do, you can whichever is appropriate of

h = get(gco)
h = get(gca)
h = get(gcf)
For example, if I want to get a handle for the xlabel of my plot, I can click on the text there, and then type

h = get(gco)
If I want to get a handle for the line I've plotted on my set of axes (remember, this is the child of gca), I can click on the line and do the same thing I did with the xlabel, *or* I can type

h = get(gca,'Children')
Once you have a handle, you can list all the properties of that handle with

or you can list a specific property, if you know which one to look for, with get(h,'PropertyName'). One useful property, for a plot of some data, is XData. If you have a handle for the line part of the plot (from get(gca,'Children'), you can use

x = get(h,'XData');
y = get(h,'YData');
to get back the data you've plotted and modify it somehow.

To reset a property to a different value, you use the set command. If you simply type

you will get back a list of the options you can set that property to. For something like 'XData', you will be told that there is no fixed set of property values (obviously, you can use anything for the x data of a plot). For something like 'XScale', which tells you the scale of the x axis, if you type
you will get back [ {linear} | log ], which indicates that the two options are linear or log, and it is currently linear.

To change the value of a property, and actually modify the plot, you would do

In the example above, to change the scale of the current x axis to log, you would type

You can use these features to make just about any modification you might want to do to a graph (including all the ones in the problem set...), if you find the right property to change. The basic approach is to get a list of the properties until you find the right one, get a handle to the object that has that property, and then set the property to a new value using the handle.